Android O-旧的启动前台服务仍在工作吗? [英] Android O - Old start foreground service still working?
问题描述
因此,在Android O上,如果您希望每小时收到的位置更新不止几个,则需要将服务作为前台服务运行.
So, with Android O, you need to have your service running as a foreground service if you want to receive more than just a few location updates per hour.
我注意到启动前台服务的旧方法似乎可以在O上运行. 即
I noticed that the old method for starting a foreground service does seem to work on O. i.e.
startForeground(NOTIFICATION_ID, getNotification());
根据此处的行为更改指南: https://developer.android.com/preview/behavior-changes.html
According to the behaviour changes guide here: https://developer.android.com/preview/behavior-changes.html
NotificationManager.startServiceInForeground()方法启动前台服务.启动前台服务的旧方法不再起作用.
The NotificationManager.startServiceInForeground() method starts a foreground service. The old way to start a foreground service no longer works.
尽管新方法仅适用于O,但似乎旧方法仍然适用于O设备(无论是否针对O).
Though the new method only works when targeting O, it seems that the old method still seems to work on an O device whether targeting O or not.
修改 包括示例:
Google示例项目LocationUpdatesForegroundService实际上有一个有效的示例,您可以在其中直接看到问题. https://github.com/googlesamples/android-play-location/tree /master/LocationUpdatesForegroundService
The Google sample project LocationUpdatesForegroundService actually has a working example where you can see the issue first hand. https://github.com/googlesamples/android-play-location/tree/master/LocationUpdatesForegroundService
无论是针对API级别25进行定向和编译,还是针对O进行定向和编译(如此处指示:
The startForeground method seems to work without issue whether targeting and compiling against API level 25 OR targeting and compiling against O (as directed to here: https://developer.android.com/preview/migration.html#uya)
因此,要重现:
- 按照上一链接所述配置应用程序gradle
- 打开应用
- 请求位置更新
- 关闭应用程序(通过后退"按钮或主页"按钮)
服务在前台运行(通过通知阴影中的图标显示).即使在运行O的设备上,位置更新也会按预期(每10秒)进行.我在这里缺少什么?
Service is running in foreground (shown by icon in notification shade). Location updates are coming through as expected (every 10 seconds) even on a device running O. What I am missing here?
推荐答案
这对我有用.
- 在活动"类中,使用 startForegroundService()而不是 startService() 启动服务
- In Activity class, start service using startForegroundService() instead of startService()
Intent myService = new Intent(this, MyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(myService);
} else {
startService(myService);
}
- 现在在 onStartCommand()中的Service类中执行以下操作
- Now in Service class in onStartCommand() do as following
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
......
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder builder = new Notification.Builder(this, ANDROID_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText(text)
.setAutoCancel(true);
Notification notification = builder.build();
startForeground(1, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText(text)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
Notification notification = builder.build();
startForeground(1, notification);
}
return START_NOT_STICKY;
}
注意:使用 Notification.Builder 代替 NotificationCompat.Builder 使其正常工作.仅在Notification.Builder中,您需要提供Channel ID,这是Android Oreo中的新功能.
Note: Using Notification.Builder instead of NotificationCompat.Builder made it work. Only in Notification.Builder you will need to provide Channel ID which is new feature in Android Oreo.
希望它能起作用!
这篇关于Android O-旧的启动前台服务仍在工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!