I am devloping a simple app that notifies the user when the SMS modified from any application I am using content observer the problem is that I want to run it even if my app is off so if my app is off and some user mark a SMS as read he should get notfication that 1 SMS modified
here is my code
public class SMSObserver extends ContentObserver
{
SMSLogger smsLogger;
Context context;
public SMSObserver(SMSLogger smsLogger, Context context) {
super(new Handler());
this.context=context;
this.smsLogger = smsLogger;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
smsLogger.querySMS(context);
}
}
public class SMSLogger {
protected void querySMS(Context context) {
Uri uriSMS = Uri.parse("content://sms/");
Cursor cur = context.getContentResolver().query(uriSMS, null, null, null, null);
/* cur.moveToNext(); // this will make it point to the first record, which is the last SMS sent
String body = cur.getString(cur.getColumnIndex("body")); //content of sms
String add = cur.getString(cur.getColumnIndex("address")); //phone num
String time = cur.getString(cur.getColumnIndex("date")); //date
String protocol = cur.getString(cur.getColumnIndex("protocol")); //protocol*/
Toast.makeText(context, "Data Changed CHECK SMS" , Toast.LENGTH_SHORT).show();
/*logging action HERE...*/
}
}
and here is the registration code
any idea where to add these lines of code so the user always got notified when any SMS updated ?
Toast.makeText(this.getBaseContext(), "Data Changed CHECK SMS", Toast.LENGTH_SHORT).show();
final Uri SMS_STATUS_URI = Uri.parse("content://sms");
SMSLogger sl= new SMSLogger();
SMSObserver smsSentObserver = new SMSObserver(sl, this.getBaseContext());
getContentResolver().registerContentObserver(SMS_STATUS_URI, true, smsSentObserver);
You can Create a service and let it run on the foreground to avoid the random killing by the system
private void startServiceAsForeground() {
Intent intent = new Intent(MyServiceService.this, targetActivity.class);
startForeground(ONGOING_NITIFICATION_ID, creatOngoingNotificationObject(getString(R.string.ongoing_message_ok), intent));
}
private void updateServiceForegroundMessage(String message, Intent intent) {
startForeground(ONGOING_NITIFICATION_ID, creatAntiDistractionOngoingNotificationObject(message, intent));
}
private android.app.Notification creatOngoingNotificationObject(String messageBody, Intent intent) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MyServiceService.this).setSmallIcon(R.drawable.ic_launcher_gray)
.setContentTitle(getString(R.string.app_name)).setAutoCancel(false).setOngoing(true).setContentText(messageBody);
// Intent intent = new Intent(MainService.this, intentClass );
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(MyServiceService.this, 0, intent, 0);
mBuilder.setContentIntent(pIntent);
return mBuilder.build();
}
And insert your code to the service and then you are guaranteed to intercept any incomming/outgoing message even if your application is not on the foreground because your service will be running all the time and the user will be aware of it .