简体   繁体   中英

Android BroadcastReceiver's onReceive sometimes not called

My apps uses BroadcastReceiver with Geofencing API . The BroadcastReceiver will be called when the user entering a Geofence .

Yesterday, the code works flawlessly. The BroadcastReceiver detect me entering a Geofence which i set around my place by this code :

    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);

The Geofence setExpirationDuration(Geofence.NEVER_EXPIRE) and radius is 1609 Meter (1 Mile)

Today, i uninstalled the apps and run the exact same code again, on the same computer and same device . I stumbled upon the fact my BroadcastReceiver 's onReceive never getting called.

I checked the internet connection which is fine. I tried to clean , rebuild , and make project on the android studio and manually uninstall the APK before running the android studio but the BroadcastReceiver still not working.

There is an important point i found out :

The breakpoint i put inside onReceive is enabled (plain red) but not valid (red with checkmark). This is the link of what i mean.

Yesterday (when the exact same code is working), the breakpoint is valid .

Then i tried to use the IntentService instead of BroadcastReceiver (i will share the code of both of them later in this question). The IntentService is called hence my PendingIntent is working.

After that, i tried to use the BroadcastReceiver again, and the BroadcastReceiver is working perfectly like yesterday. Now, sometimes the BroadcastReceiver is called sometimes not.

The strange part is i need to change the PendingIntent to IntentService everytime my BroadcastReceiver not working to make my BroadcastReceiver start working again.

And once it start working again, the BroadcastReceiver still called even with smaller (250 Meter) radius . So i think the radius is not the problem.

I tried to debug the onReceive and found out the breakpoint must be valid (not just enabled ) to make my BroadcastReceiver working. In other words, if the breakpoint is just enabled (not valid ) then the onReceive will never be called.

This is how i call the BroadcastReceiver :

    Intent intent = new Intent("mypackage.ACTION_RECEIVE_GEOFENCE");
    PendingIntent geofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    LocationServices.GeofencingApi.addGeofences(
            mGoogleApiClient,
            getGeofencingRequest(),
            geofencePendingIntent
    ).setResultCallback(this);

And this is how i call the IntentService :

        Intent intent = new Intent(this, GeofenceUpdateService.class);
        PendingIntent geofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        LocationServices.GeofencingApi.addGeofences(
                mGoogleApiClient,
                getGeofencingRequest(),
                geofencePendingIntent
        ).setResultCallback(this);

And this is my manifest , which contain both of IntentService and BroadcastReceiver :

        <service android:enabled="true" android:name="mypackage.GeofenceUpdateService" />

        <receiver android:name="mypackage.GeofenceUpdateReceiver"
            android:exported="false">
            <intent-filter >
                <action android:name="mypackage.ACTION_RECEIVE_GEOFENCE"/>
            </intent-filter>
        </receiver>

I do not post the onReceive or onHandleIntent because the problem is not inside those code, but why those method sometimes not getting called .

How to make my BroadcastReceiver work more stable? (always working)

Or is there any workaround? Should i just use IntentService instead?

Thanks a lot for your time

Check this answer as it seems that you are not familiar with how Android works Do I need to acquire wake lock when invoking a BroadcastReceiver .

And then use WakefulBroadcastReceiver instead of BroadcastReceiver and check if it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM