我们下面这篇帖子主要讲的就是。完成后的效果:运行例子,会在手机的状态栏显示一个通知的提示图案,并有你自己定义的通知声音,然后当点击了这个事件,跳转到另一个activity中,同时通知的提示图案消失.

  1.获取通知管理器

Java代码:
  1. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
复制代码
2.创建一个通知,指定其图标和标题,创建新的activity,发布通知

Java代码:
  1. int icon = android.R.drawable.stat_notify_chat;
  2. long when = System.currentTimeMillis();
  3. // 第一个参数为图标,第二个参数为标题,第三个为通知时间

  4. Notification notification = new Notification(icon, null, when);
  5. Intent openintent = new Intent(this, OtherActivity.class);

  6. // 当点击消息时就会向系统发送openintent意图
  7. PendingIntent contentIntent = PendingIntent.getActivity(this, 0,openintent, 0);
  8. notification.setLatestEventInfo(this, "标题", "内容", contentIntent);
  9. mNotificationManager.notify(0, notification);
复制代码 3.新建一个方法 用来播放提示音

Java代码:
  1. private MediaPlayer ring() throws Exception, IOException {
  2. // TODO Auto-generated method stub
  3. Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  4. MediaPlayer player = new MediaPlayer();
  5. player.setDataSource(this, alert);
  6. final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  7. if (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) != 0) {
  8. player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);

  9. player.setLooping(true);

  10. player.prepare();

  11. player.start();

  12. }

  13. return player;
  14. }

更多相关文章

  1. Android中ContentProvider的工作过程
  2. Android(安卓)代码中设置EditText的hint字体大小
  3. Android(安卓)中使用极光推送消息详细介绍(一)
  4. Android(安卓)Studio 构建变体(Build Variant)
  5. 免费Android客户端静态代码安全分析工具—Dexter
  6. Android(安卓)Studio开发界面自定义,更有利于开发
  7. Android中ViewFlipper实现动态加载view
  8. Android控件之Spinner用法详解
  9. NotificationCompat.Builder过时和通知显示不了的解决办法

随机推荐

  1. PMP项目管理:PMIID与PDU数量速查
  2. 分享 9 个 JavaScript 可视化工具
  3. 深度好文 |Matplotlib 可视化最有价值的 5
  4. Oracle基本操作+Java连接Oracle
  5. Python英语 - Issue16
  6. 思科dhcp和链路捆绑
  7. 2019年1-5月文章汇总 | Python数据之道
  8. Android中的UI线程详解
  9. 可视化神器 Plotly Express 合并到 Plotl
  10. JVM的新生代跟老年代