Android 地理围栏在 OnePlus、小米等手机中不触发
Android Geofencing Not Triggering in some mobile like OnePlus, Xiaomi etc
我正在根据 Android 文档的指南使用地理围栏代码。在索尼 XA1、三星 J7 Nxt、小米 5A、Poco f1、OnePlus 6 等真实设备上进行测试。地理围栏 Enter 和 Exit 正在运行妥妥的索尼XA1,三星J7 Nxt。
小米 & OnePlus 手机中的问题。
- 在小米5A 有时进入正常但退出不触发
- 在小米Poco f1中进入和退出都不起作用
- 在 OnePlus Mobile 中,仅当应用程序打开时才有效。
地理围栏代码:
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 设备上,必须禁用应用的电池优化,即必须在“设置”>“电池”>“所有应用”>“YourApp”中将其设置为 'Dont optimize'。只有这样,它才能在您的应用程序处于后台或什至不处于后台时运行。
在小米设备上,您的应用必须在设置中启用自动启动权限,地理围栏才能正常工作。
大多数其他中国设备,如联想、酷派等,在应用程序被最近杀死后也没有触发任何地理围栏转换事件。
您可以将用户重定向到设置中的特定页面,并告诉他们 enable/disable 他们以使地理围栏正常工作。
除此之外我还没有找到任何解决方案。
你也可以查看这些Geofence Troubleshing
避免使用广播接收器,因为这些是 Oreo 及更高版本的电池优化目标。所以使用 fusedLocationProvider 是推荐的方式。另外,不要调用地理围栏 API,最好使用 Haversine 公式(用于计算坐标之间的距离)
LatLng Home = new LatLng(10.398152, 76.096864);
int Radius =100;
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
//Haversine formula
double longitude = location.getLongitude() - Home.longitude;
double latitude = location.getLatitude() - Home.latitude;
double a = Math.pow(Math.sin(latitude/2),2) + Math.cos(Home.latitude)
*Math.cos(location.getLatitude())*Math.pow(Math.sin(longitude/2),2);
double circuit = 2 * Math.asin(Math.sqrt(a));
double eradius = 6371;
if(circuit*eradius<Radius){
Log.e("User Status","Within Geofence");
}
else{
Log.e("User Status","User outside Geofence");
}
我正在根据 Android 文档的指南使用地理围栏代码。在索尼 XA1、三星 J7 Nxt、小米 5A、Poco f1、OnePlus 6 等真实设备上进行测试。地理围栏 Enter 和 Exit 正在运行妥妥的索尼XA1,三星J7 Nxt。
小米 & OnePlus 手机中的问题。
- 在小米5A 有时进入正常但退出不触发
- 在小米Poco f1中进入和退出都不起作用
- 在 OnePlus Mobile 中,仅当应用程序打开时才有效。
地理围栏代码:
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 设备上,必须禁用应用的电池优化,即必须在“设置”>“电池”>“所有应用”>“YourApp”中将其设置为 'Dont optimize'。只有这样,它才能在您的应用程序处于后台或什至不处于后台时运行。
在小米设备上,您的应用必须在设置中启用自动启动权限,地理围栏才能正常工作。
大多数其他中国设备,如联想、酷派等,在应用程序被最近杀死后也没有触发任何地理围栏转换事件。
您可以将用户重定向到设置中的特定页面,并告诉他们 enable/disable 他们以使地理围栏正常工作。
除此之外我还没有找到任何解决方案。
你也可以查看这些Geofence Troubleshing
避免使用广播接收器,因为这些是 Oreo 及更高版本的电池优化目标。所以使用 fusedLocationProvider 是推荐的方式。另外,不要调用地理围栏 API,最好使用 Haversine 公式(用于计算坐标之间的距离)
LatLng Home = new LatLng(10.398152, 76.096864);
int Radius =100;
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
//Haversine formula
double longitude = location.getLongitude() - Home.longitude;
double latitude = location.getLatitude() - Home.latitude;
double a = Math.pow(Math.sin(latitude/2),2) + Math.cos(Home.latitude)
*Math.cos(location.getLatitude())*Math.pow(Math.sin(longitude/2),2);
double circuit = 2 * Math.asin(Math.sqrt(a));
double eradius = 6371;
if(circuit*eradius<Radius){
Log.e("User Status","Within Geofence");
}
else{
Log.e("User Status","User outside Geofence");
}