Am using Geo-fencing code under guidelines of Android document. Tested in Real device such as Sony XA1, Samsung J7 Nxt, Xiaomi 5A, Poco f1, OnePlus 6. Geo-fencing Enter and Exit are working Properly in Sony XA1, Samsung J7 Nxt.
Issues in Xiaomi & OnePlus Mobile.
Geo-Fencing Code:
private GeofencingClient mGeofencingClient;
private ArrayList<Geofence> mGeofenceList;
private PendingIntent mGeofencePendingIntent;
mGeofenceList = new ArrayList<>();
mGeofencingClient = LocationServices.getGeofencingClient(getApplicationContext());
//Latitude & Longitude Comes from Array to Add
mGeofenceList.add(new Geofence.Builder().setRequestId(String.valueOf(mall.mallId)).setCircularRegion(
mall.latitude,
mall.longitude,
mall.geofencingMeters)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(mGeofenceList);
return builder.build();
}
private PendingIntent getGeofencePendingIntent() {
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
mGeofencePendingIntent = PendingIntent.getBroadcast(this, FENCING_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
BroadcastReceiver
public class GeofenceBroadcastReceiver extends BroadcastReceiver {
/**
* Receives incoming intents.
*
* @param context the application context.
* @param intent sent by Location Services. This Intent is provided to Location
* Services (inside a PendingIntent) when addGeofences() is called.
*/
@Override
public void onReceive(Context context, Intent intent) {
// Enqueues a JobIntentService passing the context and intent as parameters
StoreFencing.enqueueWork(context, intent);
}
}
}
Fencing Service:
public class StoreFencing extends JobIntentService {
private static final int JOB_ID = 502;
List<Geofence> triggeringGeofences;
public static void enqueueWork(Context context, Intent intent) {
enqueueWork(context, StoreFencing.class, JOB_ID, intent);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
return;
}
int geofenceTransition = geofencingEvent.getGeofenceTransition();
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
triggeringGeofences = geofencingEvent.getTriggeringGeofences();
getGeofenceEnterTransitionDetails(triggeringGeofences);
}
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
triggeringGeofences = geofencingEvent.getTriggeringGeofences();
getGeofenceExitTransitionDetails(triggeringGeofences);
}
}
}
Is there any problem in Code or Device. Inform users to enable additional setting in these mobile. Help to Solve this Issue.
Yes, I have also experienced these issues.
On One Plus devices, battery optimization for your app must be disabled, i.e. it must be set to 'Dont optimize' in the Settings > Battery > All apps > YourApp. Only then it will work when your app is in background or not even in background.
On Xiaomi devices, your app must have auto-start permission enabled in the settings for geofencing to work correctly.
Most other Chinese devices such as Lenovo, Coolpad, etc. have also not triggering any geofence transition events after the app has been killed from the recents.
You can redirect the user to the specific page in settings and tell them to enable/disable them for geofencing to work correctly.
Other than that I have not found any solution.
Also you can check these Geofence Troubleshing