【问题标题】:Receiver, that was created from IntentService, doesn't work从 IntentService 创建的接收器不起作用
【发布时间】:2016-02-28 14:28:49
【问题描述】:

我有地理围栏 IntentService。在 GEOFENCE_ENTER 我创建广播接收器。但它没有动作。

如果我在 MainActivity 中创建它 - 一切都很好。但我不需要。

我不明白当我从服务创建接收器时会发生什么。

    public class GeofenceTransitionsIntentService extends IntentService {

    ...

        @Override
        protected void onHandleIntent(Intent intent) {
           ...

           if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {

                //Create WiFi scan receiver
                WiFiScanReceiver wifiScan = new WiFiScanReceiver();
                registerReceiver(wifiScan, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

                //Scan
                WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                Boolean scanStarted = wm.startScan();
            }

        }

【问题讨论】:

    标签: android android-intentservice android-broadcastreceiver


    【解决方案1】:

    onHandleIntent() 中调用registerReceiver() 将无法正常工作。在onHandleIntent() 返回并且服务自行停止之后,您的接收器将在几微秒后消失。

    要么使用清单注册的接收器,要么使用常规的Service 而不是IntentService,管理您自己的后台线程并在完成后将服务安排到stopSelf()

    【讨论】:

      【解决方案2】:

      IntentService 将在onHandleIntent() 完成后销毁。 你应该使用Service

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多