簡體   English   中英

Android compileSdk 27​​啟動服務自動顯示通知

[英]Android compileSdk 27 start service automatically show notification

我有一個啟動服務的應用程序。 我知道Android Oreo有一些限制,因此我使用以下代碼啟動了服務:

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

在beaconService類的onCreate()方法中,我稱為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);

    }

我的問題是,當啟動此服務時,我收到一個空通知,表明我的應用程序正在運行。 為什么? 當我的應用程序在前台運行時,我不希望收到此通知。

服務啟動后startForeground (int id, Notification notification) startForegroundService隱式調用startForeground (int id, Notification notification) 您看到的通知是由於此隱式調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM