如何解决Android BroadcastReceiver 仅在使用需要 GPS 的应用程序时触发
我正在用 Java 开发一个 Android 应用程序,允许用户登录和查看他们附近的任何已审核的摄影地点。
打开地图片段时,所有位置都从 Firestore 数据库加载。加载后,将为所有位置创建地理围栏。我有一个 onSuccessListener,它会在添加地理围栏时记录,所以我知道这是有效的。
我想要做的是让我的应用程序在用户进入地理围栏时向用户发送通知并显示吐司,无论应用程序是打开还是关闭。但是,我只会在我的应用打开或其他主动使用 GPS 的应用(即 Google 地图)打开时收到这些通知。
我了解模拟器中存在一个错误,这意味着要在模拟器中进行测试,您必须打开 Google 地图。正如预期的那样,这是有效的。但是,我在运行 Android 10 (SDK 29) 的真实设备上也遇到了同样的问题。
我的自定义广播接收器如下:
public class GeofencebroadcastReceiver extends broadcastReceiver {
private static final String TAG = "GeofencebroadcastReceiver";
@Override
public void onReceive(Context context,Intent intent) {
NotificationUtils notificationUtils = new NotificationUtils(context);
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if(geofencingEvent.hasError()) {
Log.e(TAG,"onReceive: Error receiving geofence event...");
return;
}
List<Geofence> geofenceList = geofencingEvent.getTriggeringGeofences();
for(Geofence geofence : geofenceList) {
Log.d(TAG,"onReceive: " + geofence.getRequestId());
}
Location location = geofencingEvent.getTriggeringLocation();
int transitionType = geofencingEvent.getGeofenceTransition();
if(transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) {
Toast.makeText(context,"Geofence Entered...",Toast.LENGTH_SHORT).show();
notificationUtils.sendHighPriorityNotification("Location Entered!","location has been entered",MainActivity.class);
} else if (transitionType == Geofence.GEOFENCE_TRANSITION_DWELL) {
Toast.makeText(context,"Geofence Dwelled...",Toast.LENGTH_SHORT).show();
notificationUtils.sendHighPriorityNotification("Location Dwelled!","location has been dwelled",MainActivity.class);
}
}
}
我的 GeofenceUtils 类是:
public class GeofenceUtils extends Contextwrapper {
private static final String TAG = "GeofenceUtils";
PendingIntent pendingIntent;
public GeofenceUtils(Context base) {
super(base);
}
public GeofencingRequest getGeofencingRequest(Geofence geofence) {
return new GeofencingRequest.Builder()
.addGeofence(geofence)
.setinitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.build();
}
public Geofence getGeofence(String id,LatLng latLng,float radius,int transitionTypes){
return new Geofence.Builder()
.setCircularRegion(latLng.latitude,latLng.longitude,radius)
.setRequestId(id)
.setTransitionTypes(transitionTypes)
.setLoiteringDelay(0)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
}
public PendingIntent getPendingIntent() {
if(this.pendingIntent != null) {
return this.pendingIntent;
}
Intent intent = new Intent(this,GeofencebroadcastReceiver.class);
pendingIntent = PendingIntent.getbroadcast(this,1010,intent,PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
public String getErrorString(Exception e)
{
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
switch (apiException.getStatusCode()){
case GeofenceStatusCodes
.GEOFENCE_NOT_AVAILABLE:
return "GEOFENCE_NOT_AVAILABLE";
case GeofenceStatusCodes
.GEOFENCE_TOO_MANY_GEOFENCES:
return "GEOFENCE_TOO_MANY_GEOFENCES";
case GeofenceStatusCodes
.GEOFENCE_TOO_MANY_PENDING_INTENTS:
return "GEOFENCE_TOO_MANY_PENDING_INTENTS";
}
}
return e.getLocalizedMessage();
}
}
通过阅读类似的问题,我确定使用 PendingIntent.getbroadcast
而不是 PendingIntent.getService
,但我仍然没有任何运气。
任何帮助将不胜感激!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。