亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Android通知欄前臺服務的實現

瀏覽:8日期:2022-09-25 09:15:14

一、前臺服務的簡單介紹

前臺服務是那些被認為用戶知道且在系統內存不足的時候不允許系統殺死的服務。前臺服務必須給狀態欄提供一個通知,它被放到正在運行(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();}

啟動服務時,創建通知:

@Overridepublic void onCreate() { super.onCreate(); Log.e(TAG, 'onCreate'); // 獲取服務通知 Notification notification = createForegroundNotification(); //將服務置于啟動狀態 ,NOTIFICATION_ID指的是創建的通知的ID startForeground(NOTIFICATION_ID, notification);}

停止服務時,移除通知:

@Overridepublic void onDestroy() { Log.e(TAG, 'onDestroy'); // 標記服務關閉 ForegroundService.serviceIsLive = false; // 移除通知 stopForeground(true); super.onDestroy();}

判斷服務是否啟動及獲取傳遞信息:

@Overridepublic 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。

服務創建完畢,接下來就可以進行服務的啟動了,啟動前不要忘記在清單文件中進行前臺服務權限的添加:

<uses-permission android:name='android.permission.FOREGROUND_SERVICE' />

服務的啟動和停止

//啟動服務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,我也會繼續補充其它有用的知識及例子在項目上。

到此這篇關于Android通知欄前臺服務的實現的文章就介紹到這了,更多相關Android 通知欄前臺內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 免费观看性欧美毛片 | 精品国产免费久久久久久婷婷 | 国产大尺度吃奶无遮无挡 | a级特黄毛片 | 国产一区二区三区欧美精品 | 四虎现在的网址入口2022 | 黄色的视频在线免费观看 | 美国毛片亚洲社区在线观看 | a高清免费毛片久久 | 丁香六月 久久久 | 欧美在线性 | 国产片一级aaa毛片视频 | 国产制服 国产制服一区二区 | 黄网免费在线观看 | 国产伦一区二区三区免费 | 欧洲无线区一二区 | 最近免费中文字幕大全免费版视频 | 国产精品日韩欧美在线第3页 | 亚洲精品色一区色二区色三区 | 做a视频大全 | 免费观看黄色a一级录像 | 亚洲精品14p| 黄色综合网 | 亚洲国产成人久久精品影视 | 狠狠色噜噜狠狠狠狠狠色综合久久 | 特黄未满14周岁毛片 | 免费看一级黄色毛片 | 欧美国产日韩在线播放 | 欧美激情福利视频在线观看免费 | 亚洲综合伊人色一区 | 午夜亚洲视频 | 高清国产精品入口麻豆 | 日本内谢69xxxx免费播放 | 五月丁六月停停 | 国产在视频线精品视频www666 | 香港经典a毛片免费观看爽爽影院 | 成年网址网站在线观看 | 日本一级毛片片在线播放 | 久久精品国产精品亚洲20 | 国产欧美在线亚洲一区刘亦菲 | 日韩欧美视频免费观看 |