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

时间:2018-06-05 08:13:57

标签: android android-notifications android-8.1-oreo

我有一个启动服务的应用。我知道Android Oreo有一些限制,所以我用这段代码开始了我的服务:

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

在onCreate()方法的BeaconService类中,我调用了startForeground()方法。 我是这样做的:

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);

    }

我的问题是,当这个服务启动时,我得到一个空的通知,我的应用程序正在运行。为什么?当我的应用程序在前台运行时,我不想要这个通知。

1 个答案:

答案 0 :(得分:0)

服务启动后,

startForegroundService会隐式调用startForeground (int id, Notification notification)。您看到的通知是由于此隐式调用。

相关问题