前臺服務,Notification的使用

1.Android6.0 以下版本可以這麼用,在Service的onCreate方法中

public class MyService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        Intent intent = new Intent(this, SecondActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("message")
                .setContentTitle("title")
                .setContentIntent(pendingIntent);
        Notification notification = builder.build();
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // manager.notify(1, notification);
        startForeground(1,notification);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

使用notification ,區別是用startForeground方法,這樣打開的notification常駐狀態欄(除非進程或者該服務被殺死)。

打開一個不在狀態欄顯示的notification , 第一個參數id爲0即可 : startForeground(0, new Notification());
要從前臺刪除服務,需調用stopForeground()方法,需要一個布爾型參數,但是這個方法不終止服務。

如果該進程或者該服務被殺死,狀態欄的notification也會消失。

2.Android8.0 及以上版本可以這麼用,在Service的onCreate方法中

public class MyService extends Service {

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onCreate() {
        super.onCreate();
        Intent intent = new Intent(this, SecondActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        String channelID = "1";
        String channelName = "channel_name";
        NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.createNotificationChannel(channel);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("message")
                .setContentTitle("title")
                .setChannelId(channelID)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);
        Notification notification = builder.build();
        manager.notify(1, notification);
//        startForeground(1,notification);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

代碼和之前版本相比多了一個channelID 參數,Android 8.0 必須要有的,否則notification無法顯示。

Android 8.0 後臺應用想啓動服務就老老實實的加個notification給用戶看,表示你自己在後臺佔着資源,殺不殺由用戶決定,偷偷地在後臺跑沒有framework幫忙想都別想,會導致 anr+crash。

1)activity: Context.startForegroundService()

2)Service:startForeground(int id, Notification notification)(id must not be 0,notification must not be null)。

想要變成前臺服務:啓動服務要使用startForegroundService方法,然後必須要在5秒內調用startForeground方法,而且參數(id!=0&&notification!=null),否則導致 anr+crash。

Android8.0 需要設置通知欄,使服務變成前臺服務。

但是在 Android9.0 就會出現Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE。如果是啓動前臺服務的話 還需要一個權限 android.permission.FOREGROUND_SERVICE

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章