代码之家  ›  专栏  ›  技术社区  ›  ashaneen

事件总线。post()在服务中,如何从线程调用函数。背景

  •  0
  • ashaneen  · 技术社区  · 8 年前

    我正在尝试实现一个功能,当用户关闭应用程序(刷掉)时,该功能将进行一些清理。

    内部活动。班

      @Subscribe(threadMode = ThreadMode.BACKGROUND)
        public void stopSearching(MyService.StopSearching stopSearching)
        {
            Log.d(TAG, "stopSearching: ");
    
        }
    

    我的服务。班

    public class MyService extends Service {
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.d("ClearFromRecentService", "Service Started");
            return START_NOT_STICKY;
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.d("ClearFromRecentService", "Service Destroyed");
        }
    
        @Override
        public void onTaskRemoved(Intent rootIntent) {
            Log.e("ClearFromRecentService", "END");
            EventBus.getDefault().post(new StopSearching());
            stopSelf();
        }
    
        public class StopSearching {
            StopSearching() {
            }
        }
    }
    

    但我得到:

    EventBus:没有为事件类注册订阅者 通用域名格式。talkie公司。阿沙宁。firebaseapp。另外MyService$停止搜索

    EventBus:没有为事件类注册订阅者 组织。绿色机器人。事件总线。无订阅事件

    1 回复  |  直到 8 年前
        1
  •  1
  •   karandeep singh    8 年前

    问题是,当您的 onStop 函数被调用。当你的应用程序被最小化时,它将处于停止阶段,因此你的活动无法接收任何事件。我认为在这种情况下,您不能从后台线程执行任何事件,因为您的活动已从内存中删除,并且它甚至无法从EventBus接收任何回调。