从任何SMS应用程序修改任何SMS时通知用户

时间:2014-05-11 01:01:23

标签: android service sms contentobserver background-service

我正在开发一个简单的应用程序,当从任何应用程序修改SMS时通知用户 我正在使用内容观察者 问题是我想要运行它,即使我的应用程序已关闭,所以如果我的应用程序关闭并且某些用户将短信标记为已阅读,则应该获得1短信修改的信息

这是我的代码

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...*/
    }
}

这是注册码

知道在哪里添加这些代码行,以便在更新任何短信时始终收到用户的通知?

  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);

1 个答案:

答案 0 :(得分:0)

您可以创建服务并让它在前台运行,以避免系统随机查杀

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();
    }

然后将您的代码插入到服务中,然后即使您的应用程序不在前台,也可以保证拦截任何输入/传出消息,因为您的服务将一直运行,用户将会知道它。 / p>