前言


  • 短信来了需要通知,未接电话需要通知,新邮件需要通知……等等。很多时候都需要Notification


使用场景


  • 最近在做全家APP应用,当有订单未读消息时需要有个通知,而且一直响铃,通知还不能清除这样的需求,以下是我参考网络亲测通过的代码与大家分享,希望帮助更多的人,不懂的可以问我QQ:11745405

背景知识


  • 要使用Android通知必须使用到Android通知管理器:NotificationManager管理这个应用程序的通知,每个notify都有唯一标识符即ID。用于管理更新这个通知内容……
  • 当然还是需要添加相应的权限滴!比如响铃,震动……


代码解析

1.创建通知管理器

NotificationManager 是一个系统Service,必须通过getSystemService()方法来获取。

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

2.创建通知

Notification:是可以设置icon、文字、提示声音、振动等等参数。


int icon = R.drawable.wlicon;long when = System.currentTimeMillis();//时间//创建通知栏的显示CharSequence tickerText = "未读消息提醒";Notification notification = new Notification(icon, tickerText, when);notification.defaults |= Notification.DEFAULT_LIGHTS;  // 通知灯光notification.defaults |= Notification.DEFAULT_VIBRATE; // 震动notification.flags |= Notification.FLAG_NO_CLEAR;   // 通知不可以清除//notification.flags = Notification.FLAG_AUTO_CANCEL;  // 通知可以清除//notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // 系统默认铃声//notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");// 播放自定义的铃声//notification.flags |= Notification.FLAG_INSISTENT; // 声音一直响到用户相应,就是通知会一直响起,直到你触碰通知栏的时间就会停止//创建后在状态栏中通知的内容Context context = droidGap.getApplicationContext();CharSequence contentTitle = "未读消息提醒";CharSequence contentText = "您有" + Quantity + "条未读消息,请及时读取。";//点击后打开的项目   创建一个IntentIntent notificationIntent = new Intent(droidGap, MainActivity.class);PendingIntent contentIntent = PendingIntent.getActivity(droidGap, 0, notificationIntent, 0);notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);notificationManager.notify(NOTIFICATION_ID, notification);

完整例子

这里没有使用通知栏的响铃是因为用户下拉通知栏的时间那个响铃会停止,我需求是需要一直播放。所以使用了MediaPlayer


/** * 创建通知栏 * @param Quantity  数量 * @param player MediaPlayer  播放声音 */public void createNotification(String Quantity, MediaPlayer player) {//      创建通知栏NotificationManager notificationManager = (NotificationManager) droidGap.getSystemService(Context.NOTIFICATION_SERVICE);int icon = R.drawable.wlicon;long when = System.currentTimeMillis();//创建通知栏的显示CharSequence tickerText = "未读消息提醒";Notification notification = new Notification(icon, tickerText, when);notification.defaults |= Notification.DEFAULT_LIGHTS;  // 通知灯光notification.defaults |= Notification.DEFAULT_VIBRATE; // 震动notification.flags |= Notification.FLAG_NO_CLEAR;   // 通知不可以清除//notification.flags = Notification.FLAG_AUTO_CANCEL;  // 通知可以清除//notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // 系统默认铃声//notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");// 播放自定义的铃声//notification.flags |= Notification.FLAG_INSISTENT; // 声音一直响到用户相应,就是通知会一直响起,直到你触碰通知栏的时间就会停止//创建后在状态栏中通知的内容Context context = droidGap.getApplicationContext();CharSequence contentTitle = "未读消息提醒";CharSequence contentText = "您有" + Quantity + "条未读消息,请及时读取。";//点击后打开的项目   创建一个IntentIntent notificationIntent = new Intent(droidGap, MainActivity.class);PendingIntent contentIntent = PendingIntent.getActivity(droidGap, 0, notificationIntent, 0);notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);notificationManager.notify(NOTIFICATION_ID, notification);try {if (Integer.parseInt(Quantity) == 0 && player.isPlaying()) {player.reset();  // 到初始化状态,这里需要判断是否正在响铃,如果直接在开启一次会出现2个铃声一直循环响起,您不信可以尝试} else if (!player.isPlaying()) {NotificationUtil.ring(player);}} catch (Exception e) {e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.}}/** * 一直响铃 * @param droidGap * @param player * @return * @throws Exception * @throws IOException */private static MediaPlayer ring(MediaPlayer player) throws Exception, IOException {Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);//MediaPlayer player = new MediaPlayer();player.setDataSource(droidGap, alert);final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);if (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) != 0) {player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);player.setLooping(true);player.prepare();player.start();}return player;}

大功告成上图


可以去参考http://www.oschina.net/question/234345_40111写的不错!

作者:郏高阳 原文链接:http://my.oschina.net/jgy/blog/158513




更多相关文章

  1. Android(安卓)Wear:创建可穿戴应用 - 概述
  2. Android应用程序资源管理器(Asset Manager)的创建过程分析
  3. iOS 7 需要再和 Android(安卓)比比什么?
  4. Android8.1以及5.1版本识别sdcard和U盘并创建文件解决办法
  5. OpenGL ES教程II之创建多边形(原文对照)
  6. ue4 创建Android和ios ar应用
  7. android基本的数据库创建和使用
  8. Android的onCreateOptionsMenu()创建菜单Menu详解
  9. Android(安卓)studio 挂断电话 使用ITelephony.aidl

随机推荐

  1. Android的init过程详解(一)
  2. Android高手进阶教程(十九)---Android中
  3. Android之——史上最简单最酷炫的3D图片
  4. android之sqliteDatabase,sqliteOpenHelp
  5. Android基础入门教程——8.1.1 Android中
  6. 浅谈Android中MVC、MVP、MVVM模式(二)
  7. KJFrameForAndroid(安卓)1.3beta 发布,And
  8. 深刻解析 Android(安卓)的 AIDL 介面
  9. android style(样式)和theme(主题)设置
  10. Android异步处理一:使用Thread+Handler实