Android8.0 Server啟動并取消彈框

Android8.0以上server的啟動有新的方法:


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

????????????startForegroundService(new Intent(this, AreaService.class));

}else {

????????????startService(new Intent(this, AreaService.class));

}

在AreaService的onCreate和onDestroy添加下面的方法:


@Override

? ? public void onCreate() {

? ? ? ? super.onCreate();

? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

????????????StringCHANNEL_ID="myservice";

? ? ? ? ? ? NotificationChannel channel =new NotificationChannel(CHANNEL_ID,

? ? ? ? ? ? ? ? ? ? "mytest", NotificationManager.IMPORTANCE_HIGH);

? ? ? ? ? ? ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

? ? ? ? ? ? Notification notification =new NotificationCompat.Builder(this, CHANNEL_ID)

.setContentTitle("")

.setContentText("").build();

? ? ? ? ? ? startForeground(1, notification);? ?//主要這邊的不要寫startForeground(0, notification);?


?????NotificationManager mNotificationManager = (NotificationManager) ????????????????????????getSystemService(Context.NOTIFICATION_SERVICE);

? ? ? ? ? ? mNotificationManager.deleteNotificationChannel(CHANNEL_ID); //? ? 取消彈框操作

? ? ? ? }

}


@Override

public void onDestroy() {

? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

????????????stopForeground(true);

? ? }

????super.onDestroy();

}

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容