Android Notification使用方法總結(jié)
Android Notification使用方法總結(jié)
一. 基本使用
1.構(gòu)造notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(appContext) .setSmallIcon(appContext.getApplicationInfo().icon) .setWhen(System.currentTimeMillis()) .setAutoCancel(true)//當(dāng)點(diǎn)擊通知的時候會自動取消 .setContentTitle(contentTitle) .setTicker(notifyText)//狀態(tài)欄提示 .setContentText(summaryBody) .setContentIntent(pendingIntent) .setNumber(notificationNum); Notification notification = mBuilder.build();
2.顯示通知
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(notifyID, notification);
3.手機(jī)震動提醒
/** * 手機(jī)震動和聲音提示 */ public void viberateAndPlayTone(EMMessage message) { if(message != null){ if(EMChatManager.getInstance().isSlientMessage(message)){ return; } } if (System.currentTimeMillis() - lastNotifiyTime < 1000) { // received new messages within 2 seconds, skip play ringtone return; } try { lastNotifiyTime = System.currentTimeMillis(); // 判斷是否處于靜音模式 if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) { EMLog.e(TAG, "in slient mode now"); return; } EaseSettingsProvider settingsProvider = EaseUI.getInstance().getSettingsProvider(); if(settingsProvider.isMsgVibrateAllowed(message)){//檢測是否允許震動 long[] pattern = new long[] { 0, 180, 80, 120 }; vibrator.vibrate(pattern, -1); } if(settingsProvider.isMsgSoundAllowed(message)){//檢測是否允許聲音 if (ringtone == null) { Uri notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);//獲取系統(tǒng)默認(rèn)通知鈴聲 ringtone = RingtoneManager.getRingtone(appContext, notificationUri); if (ringtone == null) { EMLog.d(TAG, "cant find ringtone at:" + notificationUri.getPath()); return; } } if (!ringtone.isPlaying()) {//防止響鈴疊加 String vendor = Build.MANUFACTURER; ringtone.play(); // for samsung S3, we meet a bug that the phone will // continue ringtone without stop // so add below special handler to stop it after 3s if // needed if (vendor != null && vendor.toLowerCase().contains("samsung")) { Thread ctlThread = new Thread() { public void run() { try { Thread.sleep(3000); if (ringtone.isPlaying()) { ringtone.stop(); } } catch (Exception e) { } } }; ctlThread.run(); } } } } catch (Exception e) { e.printStackTrace(); } }
4.取消Notification
void cancelNotificaton() { if (notificationManager != null) notificationManager.cancel(notifyID);//根據(jù)ID取消,每個Notification都有唯一的ID。一般在Activity的基類的onResume調(diào)用。這樣可以達(dá)到進(jìn)入程序后,通知自動取消的效果 }
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android解析服務(wù)器端發(fā)來的xml數(shù)據(jù)示例
Android跟服務(wù)器交互數(shù)據(jù),有時數(shù)據(jù)量大時,就需要以xml形式的交互數(shù)據(jù),下面與大家分享下使用XmlPullParser來解析xml數(shù)據(jù),感興趣的朋友可以參考下哈2013-06-06Android 斷點(diǎn)續(xù)傳原理以及實現(xiàn)
這篇文章主要介紹了Android 斷點(diǎn)續(xù)傳原理以及實現(xiàn)的相關(guān)資料,這里對斷點(diǎn)續(xù)傳原理進(jìn)行了詳細(xì)介紹,需要的朋友可以參考下2016-12-12Android自定義view實現(xiàn)圓環(huán)效果實例代碼
本文通過實例代碼給大家介紹了Android自定義view實現(xiàn)圓環(huán)效果,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07Android應(yīng)用啟動流程之從啟動到可交互的過程解析
這篇文章將給大家總結(jié)學(xué)習(xí)Android 基礎(chǔ)知識,Android應(yīng)用啟動流程,從啟動到可交互的過程解析,在學(xué)習(xí)過程中,大家最好是把源碼下載下來,感興趣的小伙伴跟著小編一起來看看吧2023-08-08andoid打包短信發(fā)送到gmail郵箱實現(xiàn)代碼
andriod短信整合備份發(fā)送到gmail郵箱,需要在andoid手機(jī)配置好gmail郵箱,下面是具體的實現(xiàn)代碼,感興趣的朋友可以參考下哈2013-06-06flutter 動手?jǐn)]一個城市選擇citypicker功能
在一些項目開發(fā)中經(jīng)常會用到城市選擇器功能,今天小編動手?jǐn)]一個基于flutter 城市選擇citypicker功能,具體實現(xiàn)過程跟隨小編一起看看吧2021-08-08Android對話框自定義標(biāo)題 對話框標(biāo)題美化操作
這篇文章主要為大家詳細(xì)介紹了Android對話框自定義標(biāo)題的相關(guān)資料,如何對對話框標(biāo)題進(jìn)行美化操作,感興趣的小伙伴們可以參考一下2016-08-08