Android通知欄前台服務
一、前台服務的簡單介紹
前台服務是那些被認為使用者知道且在系統記憶體不足的時候不允許系統殺死的服務。前台服務必須給狀态欄提供一個通知,它被放到正在運作(Ongoing)标題之下——這就意味着通知隻有在這個服務被終止或從前台主動移除通知後才能被解除。
最常見的表現形式就是音樂播放服務,應用程式背景運作時,使用者可以通過通知欄,知道目前播放内容,并進行暫停、繼續、切歌等相關操作。
二、為什麼使用前台服務
背景運作的Service系統優先級相對較低,當系統記憶體不足時,在背景運作的Service就有可能被回收,為了保持背景服務的正常運作及相關操作,可以選擇将需要保持運作的Service設定為前台服務,進而使APP長時間處于背景或者關閉(程序未被清理)時,服務能夠保持工作。
三.前台服務的詳細使用
建立服務内容,如下(四大元件不要忘記清單檔案進行注冊,否則啟動會找不到服務);
public class ForegroundService extends Service {
private static final String TAG = ForegroundService.class.getSimpleName();
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "onBind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
}
}
建立服務通知内容,例如音樂播放,藍牙裝置正在連接配接等:
/**
-
建立服務通知
*/
private Notification createForegroundNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 唯一的通知通道的id.
String notificationChannelId = "notification_channel_id_01";
// Android8.0以上的系統,建立消息通道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//使用者可見的通道名稱
String channelName = "Foreground Service Notification";
//通道的重要程度
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, channelName, importance);
notificationChannel.setDescription("Channel description");
//LED燈
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
//震動
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannelId);
//通知小圖示
builder.setSmallIcon(R.drawable.ic_launcher);
//通知标題
builder.setContentTitle("ContentTitle");
//通知内容
builder.setContentText("ContentText");
//設定通知顯示的時間
builder.setWhen(System.currentTimeMillis());
//設定啟動的内容
Intent activityIntent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
//建立通知并傳回
return builder.build();
啟動服務時,建立通知:
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate");
// 擷取服務通知
Notification notification = createForegroundNotification();
//将服務置于啟動狀态 ,NOTIFICATION_ID指的是建立的通知的ID
startForeground(NOTIFICATION_ID, notification);
停止服務時,移除通知:
public void onDestroy() {
Log.e(TAG, "onDestroy");
// 标記服務關閉
ForegroundService.serviceIsLive = false;
// 移除通知
stopForeground(true);
super.onDestroy();
判斷服務是否啟動及擷取傳遞資訊:
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
// 标記服務啟動
ForegroundService.serviceIsLive = true;
// 資料擷取
String data = intent.getStringExtra("Foreground");
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
以上就是前台服務的建立過程,相關注釋已經很明白了,具體使用可以檢視文末的Demo。
服務建立完畢,接下來就可以進行服務的啟動了,啟動前不要忘記在清單檔案中進行前台服務權限的添加:
服務的啟動和停止
//啟動服務
if (!ForegroundService.serviceIsLive) {
// Android 8.0使用startForegroundService在前台啟動新服務
mForegroundService = new Intent(this, ForegroundService.class);
mForegroundService.putExtra("Foreground", "This is a foreground service.");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(mForegroundService);
} else {
startService(mForegroundService);
}
} else {
Toast.makeText(this, "前台服務正在運作中...", Toast.LENGTH_SHORT).show();
//停止服務
mForegroundService = new Intent(this, ForegroundService.class);
stopService(mForegroundService);
關于前台服務的介紹及使用就到這裡了,相關使用已上傳至Github開發記錄,歡迎點選查閱及Star,我也會繼續補充其它有用的知識及例子在項目上。
原文位址
https://www.cnblogs.com/jqnl/p/12599905.html