简体   繁体   English

Android compileSdk 27​​启动服务自动显示通知

[英]Android compileSdk 27 start service automatically show notification

I have an app that start a service. 我有一个启动服务的应用程序。 I know that Android Oreo has some limitations, so I started my service with this code: 我知道Android Oreo有一些限制,因此我使用以下代码启动了服务:

context.startForegroundService(new Intent(context.getApplicationContext(), BeaconService.class));

In the BeaconService class in the onCreate() method I called startForeground() method. 在beaconService类的onCreate()方法中,我称为startForeground()方法。 I did it like this: 我这样做是这样的:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                "MyApp",
                NotificationManager.IMPORTANCE_HIGH);

        try {
            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
        } catch (Exception e){
            e.printStackTrace();
        }

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID).build();
        startForeground(1, notification);

    }

My problem is that when this service is starting I got an empty notification that my app is running. 我的问题是,当启动此服务时,我收到一个空通知,表明我的应用程序正在运行。 Why? 为什么? I don't want this notification when my app is running in foreground. 当我的应用程序在前台运行时,我不希望收到此通知。

startForegroundService implicitly calls startForeground (int id, Notification notification) as soon as the service starts. 服务启动后startForeground (int id, Notification notification) startForegroundService隐式调用startForeground (int id, Notification notification) The notification you are seeing is because of this implicit call. 您看到的通知是由于此隐式调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM