0

我有一个启动服务的应用程序。我知道 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);

    }

我的问题是,当此服务启动时,我收到一个空通知,表明我的应用程序正在运行。为什么?当我的应用程序在前台运行时,我不希望收到此通知。

4

1 回答 1

0

startForegroundServicestartForeground (int id, Notification notification)服务启动后立即隐式调用。您看到的通知是由于这个隐式调用。

于 2018-06-05T08:21:53.833 回答