Java android oreo的后台服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47110489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:33:56  来源:igfitidea点击:

Background service for android oreo

javaandroidpush-notificationandroid-serviceandroid-8.0-oreo

提问by faiyaz meghreji

How to continue background service in android Oreo without showing notification dot? i continues my background service using notification but i don't want to show notification for running service.

如何在不显示通知点的情况下在 android Oreo 中继续后台服务?我使用通知继续我的后台服务,但我不想显示正在运行的服务的通知。

采纳答案by Amrut Bidri

If you would have read the Android Oreo 8.0Documentation properly somewhere in here, you might not have posted this question here.

如果您在此处的某处正确阅读了Android Oreo 8.0文档,则您可能没有在此处发布此问题。

Step 1:Make sure you start a service as a foreground Serviceas given in below code

第 1 步:确保将服务作为前台启动,Service如下面的代码所示

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 onCreatemethod of Service.

第 2 步:使用通知显示您的服务正在运行。在 的onCreate方法中添加以下代码行Service

@Override
public void onCreate() {
    ...
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForeground(NOTIFICATION_ID, notification);
    }
    ...
}

Step 3:Remove the notificationwhen the service is stopped or destroyed.

第 3 步:notification在服务停止或销毁时删除。

@Override
public void onDestroy() {
    ...
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          stopForeground(true); //true will remove notification
    }
    ...
}

One problem with this solution is that it will keep showing the notificationuntil your Serviceis running on all devices running on Android Oreo 8.0.

此解决方案的一个问题是它会一直显示notification直到您Service在运行Android Oreo 8.0 的所有设备上运行。

I'm sure that this solution will work even when the app is in the background or in kill state.

我确信这个解决方案即使在应用程序处于后台或终止状态时也能正常工作。

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 AWAYWITH IT. SO IT IS RECOMMENDEDTHAT YOU MAKE NECESSARY CHANGES IN YOUR APP TO MAKE IT WORK PROPERLY AS PER THE BEST PRACTICESFOLLOWED OR ASKED BY ANDROID.

重要说明:在 ANDROID OREO 8.0 中必须显示在后台运行服务的通知(应用程序处于后台或已终止状态)。你不能逃避它。因此,建议您根据 ANDROID 遵循或要求的最佳实践,对您的应用程序进行必要的更改以使其正常工作。

I hope this might help to solve your problem.

我希望这可能有助于解决您的问题。

回答by Abdur Rahman

This isn't possible in this API version (26)and higher. Android OS automatically close your service if you run it without showing a notification to the user.

这在这个API version (26)和更高版本中是不可能的。如果您在不向用户显示通知的情况下运行它,Android 操作系统会自动关闭您的服务。

If you are targeting API >= 26the system will impose a restriction on your service to run in background unless your activity in foreground. As soon as your activity goes to background, the service will be terminates when system finds it running in background (See Background service limitations).

如果您的目标API >= 26是系统将限制您的服务在后台运行,除非您的活动在前台。一旦您的活动进入后台,当系统发现它在后台运行时,该服务将终止(请参阅后台服务限制)。

By using startForegroundService()method you grant that permission to run the service in background even when the activity isn't running. Must also once the service has been created, the service must call its startForeground()method within five seconds.

通过使用startForegroundService()方法,即使活动未运行,您也可以授予在后台运行服务的权限。还必须在服务创建后,服务必须startForeground()在五秒内调用其方法。

回答by Masoud Siahkali

This code worked for me. First you make sure :

这段代码对我有用。首先你要确保:

1- Define the service in Manifest

1- 在 Manifest 中定义服务

2- Define the permission in Manifest

2- 在清单中定义权限

  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

3- Make sure you have started the service before.

3-确保您之前已经启动了该服务。

Your service should have a notification. You should be careful that your notification must have a ChannelID !

this page can help you:

How to create a Custom Notification Layout in android?

Android how to show notification on screen

您的服务应该有一个通知。你应该注意你的通知必须有一个 ChannelID !

此页面可以帮助您:

如何在android中创建自定义通知布局?

Android 如何在屏幕上显示通知

Now paste the following code into your service

现在将以下代码粘贴到您的服务中

@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());
            }
    }

after that add this code:

之后添加此代码:

@Override
public ComponentName startForegroundService(Intent service) {
    return super.startForegroundService(service);
}

and this:

和这个:

@Override
public void onDestroy() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        stopForeground(true);
    }
}