contentobserver仅适用于插入和删除,但不是更新 [英] contentobserver works only for insert and delete but not for an update

查看:296
本文介绍了contentobserver仅适用于插入和删除,但不是更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序,通知用户,当任何短信标记为已读,即使应用程序没有运行

I am developing an app that notify the user when any SMS marked as read even if the app isn't running

我只是创造了一个contentobserver,我把它注册在服务

I simply created a contentobserver and I registered it in a service

问题是,如果新的SMS插入或删除,但contentobserver运行时标记为已读(更新操作)在SMS它不工作

the problem is that the contentobserver runs if the new SMS inserted or deleted but when the SMS marked as read ( Update operation) it doesn't work

这是我的服务,code

here is my service code

    public class Smssendservice extends Service {

    private static Timer timer = new Timer();
    private Context ctx;

    public IBinder onBind(Intent arg0)
    {
        return null;
    }

    public void onCreate()
    {
        super.onCreate();
        ctx = this;
        startService();
    }

    private void startService()
    {
        //timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
        Toast.makeText(getApplicationContext(), "Before Register", Toast.LENGTH_SHORT).show();
        final Uri SMS_STATUS_URI = Uri.parse("content://sms");
        SMSLogger sl= new SMSLogger();

        SMSObserver smsSentObserver = new SMSObserver(sl, ctx);
        getContentResolver().registerContentObserver(SMS_STATUS_URI, true, smsSentObserver);
        Toast.makeText(getApplicationContext(), "After Register", Toast.LENGTH_SHORT).show();
    }
}

我注册我的内容观察员服务

I am registering my content observer in the service

下面是内容观察者code

here is the content observer 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);
    }
} 

最终这里的短信记录,我显示TOAST如果SMS数据修改

eventually here is the SMS logger that I show the TOAST if the SMS data changed

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

这表明此消息数据改变检查短信如果有新的短信插入或短信删除,但在更新时犯规出现在致祝酒辞。任何线索?

it showed this message "Data Changed CHECK SMS" if new SMS inserted or SMS deleted but in case of update the toast doesnt appear. any clue ?

推荐答案

在您的更新方法,检查更新条目数大于0。
如果是,这样做的getContext()getContentResolver()有NotifyChange(URI,空)。返回更新的记录数之前。

In your update method, check if the number of entries updated is more than 0. If it is, do getContext().getContentResolver().notifyChange(uri, null); before you return the number of entries updated.

这篇关于contentobserver仅适用于插入和删除,但不是更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆