Android Notification 详解,MediaPlayer 一直播放系统铃声

http://www.open-open.com/lib/view/open1378263855687.html

背景知识


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


代码解析

1.创建通知管理器

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

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

2.创建通知

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


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 + "条未读消息,请及时读取。" ;
//      点击后打开的项目   创建一个Intent
         Intent 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


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
      * 创建通知栏
      * @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 + "条未读消息,请及时读取。" ;
//      点击后打开的项目   创建一个Intent
         Intent 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;
     }
Android Notification 详解,MediaPlayer 一直播放系统铃声
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值