av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android通知欄前臺(tái)服務(wù)的實(shí)現(xiàn)

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

一、前臺(tái)服務(wù)的簡(jiǎn)單介紹

前臺(tái)服務(wù)是那些被認(rèn)為用戶知道且在系統(tǒng)內(nèi)存不足的時(shí)候不允許系統(tǒng)殺死的服務(wù)。前臺(tái)服務(wù)必須給狀態(tài)欄提供一個(gè)通知,它被放到正在運(yùn)行(Ongoing)標(biāo)題之下——這就意味著通知只有在這個(gè)服務(wù)被終止或從前臺(tái)主動(dòng)移除通知后才能被解除。

最常見的表現(xiàn)形式就是音樂播放服務(wù),應(yīng)用程序后臺(tái)運(yùn)行時(shí),用戶可以通過通知欄,知道當(dāng)前播放內(nèi)容,并進(jìn)行暫停、繼續(xù)、切歌等相關(guān)操作。

二、為什么使用前臺(tái)服務(wù)

后臺(tái)運(yùn)行的Service系統(tǒng)優(yōu)先級(jí)相對(duì)較低,當(dāng)系統(tǒng)內(nèi)存不足時(shí),在后臺(tái)運(yùn)行的Service就有可能被回收,為了保持后臺(tái)服務(wù)的正常運(yùn)行及相關(guān)操作,可以選擇將需要保持運(yùn)行的Service設(shè)置為前臺(tái)服務(wù),從而使APP長(zhǎng)時(shí)間處于后臺(tái)或者關(guān)閉(進(jìn)程未被清理)時(shí),服務(wù)能夠保持工作。

三、前臺(tái)服務(wù)的詳細(xì)使用

創(chuàng)建服務(wù)內(nèi)容,如下(四大組件不要忘記清單文件進(jìn)行注冊(cè),否則啟動(dòng)會(huì)找不到服務(wù));

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(); } }

創(chuàng)建服務(wù)通知內(nèi)容,例如音樂播放,藍(lán)牙設(shè)備正在連接等:

/** * 創(chuàng)建服務(wù)通知 */private Notification createForegroundNotification() { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 唯一的通知通道的id. String notificationChannelId = 'notification_channel_id_01'; // Android8.0以上的系統(tǒng),新建消息通道 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); //震動(dòng) 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); //通知小圖標(biāo) builder.setSmallIcon(R.drawable.ic_launcher); //通知標(biāo)題 builder.setContentTitle('ContentTitle'); //通知內(nèi)容 builder.setContentText('ContentText'); //設(shè)定通知顯示的時(shí)間 builder.setWhen(System.currentTimeMillis()); //設(shè)定啟動(dòng)的內(nèi)容 Intent activityIntent = new Intent(this, NotificationActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); //創(chuàng)建通知并返回 return builder.build();}

啟動(dòng)服務(wù)時(shí),創(chuàng)建通知:

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

停止服務(wù)時(shí),移除通知:

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

判斷服務(wù)是否啟動(dòng)及獲取傳遞信息:

@Overridepublic int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, 'onStartCommand'); // 標(biāo)記服務(wù)啟動(dòng) ForegroundService.serviceIsLive = true; // 數(shù)據(jù)獲取 String data = intent.getStringExtra('Foreground'); Toast.makeText(this, data, Toast.LENGTH_SHORT).show(); return super.onStartCommand(intent, flags, startId);}

以上就是前臺(tái)服務(wù)的創(chuàng)建過程,相關(guān)注釋已經(jīng)很明白了,具體使用可以查看文末的Demo。

服務(wù)創(chuàng)建完畢,接下來就可以進(jìn)行服務(wù)的啟動(dòng)了,啟動(dòng)前不要忘記在清單文件中進(jìn)行前臺(tái)服務(wù)權(quán)限的添加:

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

服務(wù)的啟動(dòng)和停止

//啟動(dòng)服務(wù)if (!ForegroundService.serviceIsLive) { // Android 8.0使用startForegroundService在前臺(tái)啟動(dòng)新服務(wù) 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, '前臺(tái)服務(wù)正在運(yùn)行中...', Toast.LENGTH_SHORT).show();}

//停止服務(wù)mForegroundService = new Intent(this, ForegroundService.class);stopService(mForegroundService);

關(guān)于前臺(tái)服務(wù)的介紹及使用就到這里了,相關(guān)使用已上傳至Github開發(fā)記錄,歡迎點(diǎn)擊查閱及Star,我也會(huì)繼續(xù)補(bǔ)充其它有用的知識(shí)及例子在項(xiàng)目上。

到此這篇關(guān)于Android通知欄前臺(tái)服務(wù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Android 通知欄前臺(tái)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 日韩中文字幕在线观看 | 成人免费观看网站 | 亚洲午夜精品一区二区三区他趣 | 久久久久亚洲精品 | 亚洲欧美日韩在线 | 国产高清免费视频 | 国产高清视频在线观看 | 在线观看国产www | 国产日产欧产精品精品推荐蛮挑 | 黄色a视频 | 91久久国产综合久久 | 久久久久久国模大尺度人体 | 成人小视频在线免费观看 | 国产视频中文字幕 | 不卡的av一区 | 亚洲福利免费 | 99久久久久国产精品免费 | 国产一区二区欧美 | 97精品超碰一区二区三区 | 亚洲欧洲精品在线 | 亚洲第一色站 | 亚洲一区二区三区在线播放 | 国产精品视频不卡 | 91麻豆产精品久久久久久夏晴子 | 日韩中文视频 | 日韩五月天 | 欧洲精品码一区二区三区免费看 | 久久精品超碰 | 在线黄av | 国产传媒视频在线观看 | 亚洲成人精品 | 在线成人免费视频 | 欧美精品在线免费观看 | 欧美三区| 亚洲激情在线视频 | 国产精品久久99 | 久草.com| 国产高清视频一区 | 亚洲一区国产精品 | 国产91在线精品 | 欧美精品一区二区三区四区 |