Android Notification使用方法總結
更新時間:2017年09月06日 15:14:37 作者:心中要有一片海
這篇文章主要介紹了Android Notification使用方法總結的相關資料,這里提供了四種使用方法,需要的朋友可以參考下
Android Notification使用方法總結
一. 基本使用
1.構造notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(appContext)
.setSmallIcon(appContext.getApplicationInfo().icon)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)//當點擊通知的時候會自動取消
.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.手機震動提醒
/**
* 手機震動和聲音提示
*/
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)默認通知鈴聲
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調用。這樣可以達到進入程序后,通知自動取消的效果
}
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Android RecycleView 實現(xiàn)左滑上下分層示例代碼(自定義功能)
這篇文章主要介紹了Android RecycleView 實現(xiàn)左滑上下分層示例代碼(自定義功能),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
Android?PickerScrollView滑動選擇控件使用方法詳解
這篇文章主要為大家詳細介紹了Android?PickerScrollView滑動選擇控件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
NestScrollView嵌套RecyclerView實現(xiàn)淘寶首頁滑動效果
這篇文章主要介紹了NestScrollView嵌套RecyclerView實現(xiàn)淘寶首頁滑動效果,主要實現(xiàn)淘寶首頁嵌套滑動,中間tab吸頂效果,以及介紹NestScrollView嵌套RecyclerView處理滑動沖突的方法,需要的朋友可以參考下2021-12-12
Android Studio中Run按鈕是灰色的快速解決方法
這篇文章主要介紹了Android Studio中Run按鈕是灰色的快速解決方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-03-03

