hodov
hodov

Reputation: 69

Receiver, that was created from IntentService, doesn't work

I have Geofence IntentService. On GEOFENCE_ENTER I create broadcast receiver. But it doesn't get actions.

If I created it in MainActivity - all is good. But I don't need to.

I do not understand what happens when I create the receiver from the service.

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

        }

Upvotes: 0

Views: 73

Answers (2)

dor506
dor506

Reputation: 5414

IntentService will be destroyed once onHandleIntent() finished. You should use Service

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007554

Calling registerReceiver() in onHandleIntent() will not work well. Your receiver will go away microseconds later, after onHandleIntent() returns and the service stops itself.

Either use a manifest-registered receiver, or use a regular Service instead of an IntentService, managing your own background thread and arranging to stopSelf() the service when you are done with it.

Upvotes: 1

Related Questions