致命异常:android.app.RemoteServiceException Context.startForegro



我们最近实现了前台服务,在安卓9(仅限三星设备(上,一些用户遇到了一些反复出现的崩溃,我们不知道是什么原因造成的。有人遇到过同样的问题吗?这个问题有解决办法吗?

Fatal Exception: android.app.RemoteServiceException Context.startForegroundService() did not then call Service.startForeground()
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1874)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:214)
android.app.ActivityThread.main (ActivityThread.java:7073)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:494)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:964)

AndroidManigest.xml

<service
android:name="com.sotg.base.util.GeoNotification"
android:exported="true"
android:foregroundServiceType="location" />

GeoNotificationManager.kt

override fun startService() {
val geoService = Intent(context, GeoNotification::class.java)
geoService.action = GeoNotification.START_SERVICE_ACTION
// for android Oreo and higher start as foreground
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(geoService)
} else {
context.startService(geoService)
}
}
override fun stopService() {
// Shouldn't initialize the service to stop it when the service is not running.
if (!GeoNotification.isServiceRunning()) return;
val geoService = Intent(context, GeoNotification::class.java)
geoService.action = GeoNotification.STOP_TRACKING_ACTION
runCatching { context.startService(geoService) }
.onFailure(analytics::logException)
}

GoeNotification.java

private static boolean isServiceRunning;
public static boolean isServiceRunning() {
return isServiceRunning;
}
@Override
public void onCreate() {
super.onCreate();
isServiceRunning = false;
}
@Override
public void onDestroy() {
stopTracking();
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (START_SERVICE_ACTION.equals(intent.getAction())) {
startTracking();
} else if (STOP_TRACKING_ACTION.equals(intent.getAction())) {
stopTracking();
} else {
Log.w(TAG, "Unknown action for location service");
}
// Service restarts if it gets terminated
// the original Intent is re-delivered to the onStartCommand
// method when using START_REDELIVER_INTENT.
return START_REDELIVER_INTENT;
}
private void startTracking() {
if (isServiceRunning) return;
// create notification and config as foreground only for android Oreo and higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(Constants.NOTIFY_SURVEY_GCM, createForegroundNotification(this));
}
isServiceRunning = true;
...
}
private void stopTracking() {
if (!isServiceRunning) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
}
stopSelf();
return;
}
// stop location updates on partner SDKs
....
stopSelf();
isServiceRunning = false;
}
public static Notification createForegroundNotification(Context context) {
NotificationChannelUtil.INSTANCE.getPreparedManager(context);
String channel = NotificationChannelUtil.INSTANCE.getSYSTEM_CHANNEL_ID();
// The PendingIntent to launch activity.
PendingIntent activityPendingIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainTabActivity.class), 0);
NotificationCompat.Action settingsAction = NotificationUtil.createSettingAction(context);
String title = context.getResources().getString(R.string.foreground_service_notification_title);
String desc = context.getResources().getString(R.string.foreground_service_notification_description);
String expandedText = context.getResources().getString(R.string.foreground_service_notification_description_expanded);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel)
.setContentIntent(activityPendingIntent)
.setSmallIcon(R.drawable.noti)
.setContentTitle(title)
.setContentText(desc)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(expandedText))
.addAction(settingsAction)
.setColorized(false)
.setOngoing(true);
return builder.build();
}

我在使用安卓奥利奥或更高版本的三星和华为设备时遇到了同样的问题。我也会犯同样的错误。

如果我没记错的话,由于Android Oreo及以上版本,在使用startService()startForegroundService()时需要使用startForeground()

我通过将以下代码添加到服务类的onCreate函数中解决了这个问题。我创建了一个通知,并用startForeground()启动了它。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel mNotificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE);
mNotificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
mNotificationManager.createNotificationChannel(mNotificationChannel);
Notification note = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Reboot Sequence")
.setContentText("Rescheduling Alarms")
.setSmallIcon(R.drawable.ic_alarm_24dp)
.build();
startForeground(1, note);
}

我希望这对你有帮助。

即使使用startForeground((启动通知,也会发生这种情况。此问题是由某些设备上的软件错误引起的。因此,它无法在应用程序代码中解决。唯一现实的解决方案是在游戏开发者控制台上查看崩溃和ANR,并找到有问题的设备。然后转到同一开发人员控制台上的设备管理器并排除这些设备。我看到华为Y5导致了很多这样的崩溃。

相关内容

  • 没有找到相关文章

最新更新