android 通知播放系统声音
16lz
2021-01-26
我们下面这篇帖子主要讲的就是。完成后的效果:运行例子,会在手机的状态栏显示一个通知的提示图案,并有你自己定义的通知声音,然后当点击了这个事件,跳转到另一个activity中,同时通知的提示图案消失.
1.获取通知管理器
Java代码:
2.创建一个通知,指定其图标和标题,创建新的activity,发布通知
Java代码:
Java代码:
1.获取通知管理器
Java代码:
- NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2.创建一个通知,指定其图标和标题,创建新的activity,发布通知
Java代码:
- int icon = android.R.drawable.stat_notify_chat;
- long when = System.currentTimeMillis();
- // 第一个参数为图标,第二个参数为标题,第三个为通知时间
- Notification notification = new Notification(icon, null, when);
- Intent openintent = new Intent(this, OtherActivity.class);
- // 当点击消息时就会向系统发送openintent意图
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,openintent, 0);
- notification.setLatestEventInfo(this, "标题", "内容", contentIntent);
- mNotificationManager.notify(0, notification);
Java代码:
- private MediaPlayer ring() throws Exception, IOException {
- // TODO Auto-generated method stub
- Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- MediaPlayer player = new MediaPlayer();
- player.setDataSource(this, 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中ContentProvider的工作过程
- Android(安卓)代码中设置EditText的hint字体大小
- Android(安卓)中使用极光推送消息详细介绍(一)
- Android(安卓)Studio 构建变体(Build Variant)
- 免费Android客户端静态代码安全分析工具—Dexter
- Android(安卓)Studio开发界面自定义,更有利于开发
- Android中ViewFlipper实现动态加载view
- Android控件之Spinner用法详解
- NotificationCompat.Builder过时和通知显示不了的解决办法