Android Notification使用心得
2015-12-24 11:51
459 查看
一 基本使用
构造notification
显示通知
手机震动提醒
取消Notification
构造notification
显示通知
手机震动提醒
取消Notification
一. 基本使用
1.构造notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(appContext) .setSmallIcon(appContext.getApplicationInfo().icon) .setWhen(System.currentTimeMillis()) .setAutoCancel(true)//当点击通知的时候会自动取消 .setContentTitle(contentTitle) .setTicker(notifyText)//状态栏提示 .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);//获取系统默认通知铃声 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);//根据ID取消,每个Notification都有唯一的ID。一般在Activity的基类的onResume调用。这样可以达到进入程序后,通知自动取消的效果 }
相关文章推荐
- Android 中 EventBus 的使用(2):缓存事件
- Android 图片旋转,缩放,切割,叠加处理
- 详解Android中Notification的使用方法
- 解决Android中Sharedpreferences无法存储List数据/ASimpleCache
- Android开发在任意类中获取当前屏幕宽高
- 5.Android之image控件学习
- Android相关问题的好文章整理——温故而知新,可以为师矣
- Android Studio模拟器
- 使用android studio开发android项目之二:模拟器安装
- android图片压缩
- Android自定义圆形图片
- 加载页面遮挡耗时操作任务页面--第三方开源--AndroidProgressLayout
- Android之菜单选项Menu(一)
- android ListView 九大重要属性详细分析、
- Android图形系统之Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的联系 && camera的takePicture实例
- Android SDK Manager 更新代理配置和代理清单
- Android studio 运行时出现unable to access android sdk add-on list解决办法
- Android从一个URL下载原始数据,基于byte字节
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- 安卓图片缩放