国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android Notification使用方法總結(jié)

2019-12-12 02:05:03
字體:
供稿:網(wǎng)友

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)點擊通知的時候會自動取消          .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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 乐平市| 崇文区| 白沙| 南丹县| 台中市| 玉林市| 永新县| 南陵县| 江口县| 嫩江县| 柯坪县| 云梦县| 赤城县| 瓮安县| 浦城县| 乳源| 芜湖县| 青州市| 安岳县| 沙河市| 长宁区| 河西区| 贵德县| 华亭县| 靖远县| 子长县| 贺兰县| 河源市| 陆丰市| 香河县| 剑阁县| 荆州市| 土默特左旗| 象山县| 马关县| 五原县| 葵青区| 阳山县| 宿迁市| 苏尼特右旗| 泽州县|