我Service在Android O OS上使用Class.
我计划Service在后台使用.
Android建议指出startService()应该使用startForegroundService().
如果您使用startForegroundService(),则Service抛出a Service然后调用Service错误.
这有什么问题?
service android operating-system foreground android-8.0-oreo
我们调整了我们对Oreo的持续通知,并且效果很好.现在,仅在Pie上(在Oreo设备上没有发生),我们得到了标题错误.Pie中的前台服务有什么变化我错过了吗?
这是前台服务的onCreate代码 - >
override fun onCreate() {
super.onCreate()
val notification: Notification = NotificationCompat.Builder(this, packageName)
.setSmallIcon(R.drawable.status_notification_icon)
.setContentTitle(getString(R.string.ongoing_notify_temp_title))
.setContentText(getString(R.string.ongoing_notify_temp_message))
.setGroup(AppConstants.NOTIFICATION_GROUP_ONGOING)
.setColor(ContextCompat.getColor(this, R.color.custom_blue))
.build()
startForeground(ONGOING_NOTIFY_ID, notification)
appSettings = AppSettings(this)
weatherLookUpHelper = WeatherLookUpHelper()
MyRoomDatabase.getInstance().invalidationTracker.addObserver(onChange)
retrieveCurrentLocation()
createAlarmManager()
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我们只是创建通知然后调用startForeground.关于为什么这段代码会产生标题错误的任何想法?
侧注:Fabric Crashlytics显示此崩溃仅发生在运行Pie的像素设备(像素,像素xl,像素2,像素2 xl)上
编辑:我们的清单中有前台权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Run Code Online (Sandbox Code Playgroud) 这是我的BroadcastReciever类。该类正在处理启动电话状态。
代码;
public class BroadCastRecieverBoot extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent ?ntent) {
if(Intent.ACTION_BOOT_COMPLETED.equals(?ntent.getAction()))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, MyService.class));
context.startForegroundService(new Intent(context, GPSTracker.class));
} else {
context.startService(new Intent(context, MyService.class));
context.startService(new Intent(context, GPSTracker.class));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个错误;
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
Run Code Online (Sandbox Code Playgroud)
现在,它不适用于Android Oreo。我不知道那是什么错误。
我从以下异常输出Device Monitor:
//FATAL EXCEPTION: main
//Process: com.xxx.yyy, PID: 11584
//android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
// at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
// at android.os.Handler.dispatchMessage(Handler.java:105)
// at android.os.Looper.loop(Looper.java:164)
// at android.app.ActivityThread.main(ActivityThread.java:6944)
// at java.lang.reflect.Method.invoke(Native Method)
// at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
// at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Run Code Online (Sandbox Code Playgroud)
我的应用程序中有 4 个服务,它们基本上都是这样的:
public class MyService extends Service {
private static final int forgroundId = 55544433; //Unique for each Service
private Notification notification;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) { …Run Code Online (Sandbox Code Playgroud) service android android-notifications foregroundnotification