handleMessage未从ContentObserver

时间:2017-03-17 13:46:16

标签: java android service handler observable

我不确定我是否理解这一点,我在服务中的观察者上注册了一个处理程序。观察者的onChange()在contentprovider的notifyChange()上被触发,但注册到观察者的处理程序不会调用handleMessage?这是我手动的东西吗?我使用的是错误的帖子吗?将在下面发布我的代码,任何反馈将不胜感激,谢谢!

//Service
 @Override
public int onStartCommand(Intent intent, int i, int i1) {

    Log.i(TAG, "onStartCommand");

    if (mObserver == null) {

        HandlerThread thread = new HandlerThread("Test Thread name", Process.THREAD_PRIORITY_BACKGROUND);
        thread.start();
        Looper serviceLooper = thread.getLooper();

        mObserver = new MyObserver(new OurHandler(serviceLooper));
        getContentResolver().registerContentObserver(CustomContentProvider.CONTENT_URI, true, mObserver);
    }

    return 0;
}


 //Handler
  public class OurHandler extends Handler {
    public OurHandler(Looper looper) {
        super(looper);
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        Log.i(TAG, "handleMessage");
    }
}


//onCreate in MainActivity
       Intent i = new Intent(MainActivity.this, BackgroundService.class);
    startService(i);

0 个答案:

没有答案