我在android文档的指导下使用地理围栏代码。在真实的设备上测试,如索尼xa1,三星j7 nxt,小米5a,poco f1,oneplus 6。在索尼XA1、三星J7 NXT中,地理围栏进出都正常工作。
小米和OnePlus手机的问题。
在小蜜5A一段时间内进入正常工作,但退出不触发。
在小美PFCO F1中,进出口都不工作。
在OnePlus中,移动设备仅在应用程序打开时工作。
地理围栏规范:
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;
}
广播接收机
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);
}
}
}
围栏服务:
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);
}
}
}
代码或设备有问题吗?通知用户在这些移动设备中启用其他设置。帮助解决这个问题。
最佳答案
是的,我也经历过这些问题。
在One Plus设备上,必须禁用应用程序的电池优化,即必须在“设置>电池>所有应用程序>你的应用程序”中将其设置为“不优化”。只有当你的应用程序在后台或甚至不在后台时,它才会工作。
在小米设备上,你的应用必须在设置中启用自动启动权限,才能使地理围栏正常工作。
联想(lenovo)、酷派(coolpad)等大多数中国设备在应用程序被淘汰后也没有触发任何地理围栏过渡事件。
您可以将用户重定向到“设置”中的特定页,并告诉他们启用/禁用这些页以使地理围栏正常工作。
除此之外,我没有找到任何解决办法。
你也可以检查这些Geofence Troubleshing
关于android - Android Geofencing无法在OnePlus,Xiaomi等移动设备中触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53631565/