android 奥利奥的后台服务
Background service for android oreo
如何在不显示通知点的情况下在 android Oreo 中继续后台服务?我使用通知继续我的后台服务,但我不想显示 运行 服务的通知。
这在 API version (26)
及更高版本中是不可能的。 Android OS 如果您 运行 没有向用户显示通知,则自动关闭您的服务。
如果您的目标是 API >= 26
,系统将限制您在后台 运行 的服务,除非您在前台 activity。一旦您的 activity 进入后台,当系统在后台发现它 运行 时服务将终止(参见 Background service limitations)。
通过使用 startForegroundService()
方法,即使 activity 不是 运行ning,您也可以在后台向 运行 服务授予该权限。
还必须在创建服务后,服务必须在五秒内调用其 startForeground()
方法。
如果您在 here 中正确阅读了 Android Oreo 8.0 文档,您可能不会在此处发布此问题。
Step 1: Make sure you start a service as a foreground Service
as given in below code
ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), GpsServices.class));
ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), BluetoothService.class));
ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), BackgroundApiService.class));
Step 2: Use notification to show that your service is running. Add below line of code in onCreate
method of Service
.
@Override
public void onCreate() {
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(NOTIFICATION_ID, notification);
}
...
}
Step 3: Remove the notification
when the service is stopped or destroyed.
@Override
public void onDestroy() {
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true); //true will remove notification
}
...
}
此解决方案的一个问题是它会一直显示 notification
,直到您的 Service
在 上的所有设备 运行 上都是 运行Android奥利奥8.0.
我确信即使应用程序处于后台或终止状态,此解决方案也能正常工作。
IMPORTANT NOTE: SHOWING A NOTIFICATION FOR RUNNING A SERVICE IN BACKGROUND (APP IN BACKGROUND OR KILLED STATE) IS MANDATORY IN ANDROID OREO 8.0. YOU CANNOT RUN AWAY WITH IT. SO IT IS RECOMMENDED THAT YOU MAKE NECESSARY CHANGES IN YOUR APP TO MAKE IT WORK PROPERLY AS PER THE BEST PRACTICES FOLLOWED OR ASKED BY ANDROID.
我希望这可能有助于解决您的问题。
这段代码对我有用。首先你要确保:
1- 在清单中定义服务
2-在Manifest中定义权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
3- 确保您之前已启动该服务。
Your service should have a notification. You should be careful that
your notification must have a ChannelID !
this page can help you:
Android how to show notification on screen
现在将以下代码粘贴到您的服务中
@Override
public void onCreate() {
super.onCreate();
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(" your Notification ID ", notification);
}
} catch (Exception e) {
Log.e(" Error --->> ", e.getMessage());
}
}
之后添加此代码:
@Override
public ComponentName startForegroundService(Intent service) {
return super.startForegroundService(service);
}
还有这个:
@Override
public void onDestroy() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
}
}
如何在不显示通知点的情况下在 android Oreo 中继续后台服务?我使用通知继续我的后台服务,但我不想显示 运行 服务的通知。
这在 API version (26)
及更高版本中是不可能的。 Android OS 如果您 运行 没有向用户显示通知,则自动关闭您的服务。
如果您的目标是 API >= 26
,系统将限制您在后台 运行 的服务,除非您在前台 activity。一旦您的 activity 进入后台,当系统在后台发现它 运行 时服务将终止(参见 Background service limitations)。
通过使用 startForegroundService()
方法,即使 activity 不是 运行ning,您也可以在后台向 运行 服务授予该权限。
还必须在创建服务后,服务必须在五秒内调用其 startForeground()
方法。
如果您在 here 中正确阅读了 Android Oreo 8.0 文档,您可能不会在此处发布此问题。
Step 1: Make sure you start a service as a foreground
Service
as given in below code
ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), GpsServices.class));
ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), BluetoothService.class));
ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), BackgroundApiService.class));
Step 2: Use notification to show that your service is running. Add below line of code in
onCreate
method ofService
.
@Override
public void onCreate() {
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(NOTIFICATION_ID, notification);
}
...
}
Step 3: Remove the
notification
when the service is stopped or destroyed.
@Override
public void onDestroy() {
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true); //true will remove notification
}
...
}
此解决方案的一个问题是它会一直显示 notification
,直到您的 Service
在 上的所有设备 运行 上都是 运行Android奥利奥8.0.
我确信即使应用程序处于后台或终止状态,此解决方案也能正常工作。
IMPORTANT NOTE: SHOWING A NOTIFICATION FOR RUNNING A SERVICE IN BACKGROUND (APP IN BACKGROUND OR KILLED STATE) IS MANDATORY IN ANDROID OREO 8.0. YOU CANNOT RUN AWAY WITH IT. SO IT IS RECOMMENDED THAT YOU MAKE NECESSARY CHANGES IN YOUR APP TO MAKE IT WORK PROPERLY AS PER THE BEST PRACTICES FOLLOWED OR ASKED BY ANDROID.
我希望这可能有助于解决您的问题。
这段代码对我有用。首先你要确保:
1- 在清单中定义服务
2-在Manifest中定义权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
3- 确保您之前已启动该服务。
Your service should have a notification. You should be careful that your notification must have a ChannelID !
this page can help you:
Android how to show notification on screen
现在将以下代码粘贴到您的服务中
@Override
public void onCreate() {
super.onCreate();
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(" your Notification ID ", notification);
}
} catch (Exception e) {
Log.e(" Error --->> ", e.getMessage());
}
}
之后添加此代码:
@Override
public ComponentName startForegroundService(Intent service) {
return super.startForegroundService(service);
}
还有这个:
@Override
public void onDestroy() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
}
}