问题描述
我在 Android 中使用地理围栏 API,因此我有一个 IntentService,当用户进入我创建的地理围栏时,我将其用作获取回调的触发器。
当我从系统收到有关地理围栏活动的触发器时,我创建了一个通知,以通知用户应用程序中发生的后台活动。这是我在通知上定义的 onCreate 方法。
public class GeofenceService extends IntentService {
private static final String TAG = "GeoFenceTransitions";
@Override
public void onCreate() {
super.onCreate();
LogHelper.d("GeoService","Oncreate");
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationHelper notificationHelper = new NotificationHelper(getContext(),NotificationHelper.CHANNEL_FENCE_NOTIFICATIONS,true);
Notification notification = notificationHelper.getNotificationCompatBuilderForForeground("Test","Notifcation").build();
LogHelper.d("GeoService","Oncreate notification initialised");
startForeground(notificationID,notification);
LogHelper.d("GeoService","Oncreate notification started");
} else {
LogHelper.d("GeoService",new Notification());
}
}catch (Exception e){
LogHelper.d(TAG,e.getMessage());
}
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
LogHelper.d(TAG,"onHandleIntent");
stopForeground(true);
stopSelf();
LogHelper.d("GeoService","onHandleIntent stopped");
}
我在 onHandleIntent 中执行一些操作,然后调用 stopForeground(true) 和 stopSelf()
我在配置地理围栏时创建了一个 Pending Intent 以在我的服务上触发此回调,如下所示:
private PendingIntent getGeofencePendingIntent() {
Intent intent = new Intent(applicationContext,GeofenceService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return PendingIntent.getForegroundService(applicationContext,intent,PendingIntent.FLAG_UPDATE_CURRENT);
} else {
return PendingIntent.getService(applicationContext,PendingIntent.FLAG_UPDATE_CURRENT);
}
}
现在,当在地理围栏中启动了一些触发器时,Android 系统通过调用 startForegroundService() 启动该触发器,但由于在大多数中国 OEM(Oppo、VIVO、小米)中,后台活动不允许我调用 startForgeround 的 onCreate 方法不会被触发,因此导致崩溃。
崩溃日志:
致命异常:android.app.RemoteServiceException Context.startForegroundService() 然后没有调用 Service.startForeground():ServiceRecord{520b2b3 u0 app.geofencing.GeoFenceService}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)