小编use*_*302的帖子

android 8前台服务系统通知“正在后台运行”

我有针对 android 8 的前台服务。当一些应用程序活动可见时,会有像“voip service runnig”这样的服务通知。

问题是当应用程序进入后台时,android 会显示额外的系统通知“应用程序正在后台运行”。

如何避免这种情况?用户不喜欢看到一个应用程序的两个持续通知。

仅显示服务通知的屏幕

带有服务和系统通知的屏幕

代码:

public class MyService extends Service {

    String TAG = "MyService";

    public MyService() {
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate: ");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            Notification notification = new NotificationCompat.Builder(this, TestApplication.PRIMARY_NOTIF_CHANNEL)
                    .setSmallIcon(android.R.drawable.ic_menu_call)
                    .setContentText("ServiceText")
                    .setContentTitle("ServiceTitle")
                    .setCategory(NotificationCompat.CATEGORY_SERVICE)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setOngoing(true)
                    .build();

            startForeground(PRIMARY_FOREGROUND_NOTIF_SERVICE_ID, notification);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: ");
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: …
Run Code Online (Sandbox Code Playgroud)

android foregroundnotification foreground-service android-8.0-oreo

5
推荐指数
0
解决办法
623
查看次数