首页 > 系统 > Android > 正文

Android中通过Notification&NotificationManager实现消息通知

2019-10-24 20:35:20
字体:
来源:转载
供稿:网友

关于通知Notification相信大家都不陌生了,平时上QQ的时候有消息来了或者有收到了短信,手机顶部就会显示有新消息什么的,就类似这种。今天就稍微记录下几种Notification的用法。3.0以前的通知和3.0以后的通知是有些区别的。

notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。

1、新建一个android项目

我新建项目的 minSdkVersion="11",targetSdkVersion="19"。也就是支持最低版本的3.0的。

2、习惯性地打开项目清单文件AndroidManifest.xml

,添加一个权限: 不添加不行的。

3、在布局activity_main.xml中添加几个按钮

,样子就大概这样,垂直排版的LinearLayout

具体代码

 

 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2. xmlns:tools="http://schemas.android.com/tools" 
  3. android:layout_width="match_parent" 
  4. android:layout_height="match_parent" 
  5. android:paddingBottom="@dimen/activity_vertical_margin" 
  6. android:paddingLeft="@dimen/activity_horizontal_margin" 
  7. android:paddingRight="@dimen/activity_horizontal_margin" 
  8. android:paddingTop="@dimen/activity_vertical_margin" 
  9. android:orientation="vertical" 
  10. tools:context=".MainActivity" > 
  11. <Button  
  12. android:id="@+id/btn_01" 
  13. android:layout_width="match_parent" 
  14. android:layout_height="wrap_content" 
  15. android:text="3.0以前版本的notification,用新的吧" 
  16. android:onClick="click" 
  17. /> 
  18. <Button  
  19. android:id="@+id/btn_02" 
  20. android:layout_width="match_parent" 
  21. android:layout_height="wrap_content" 
  22. android:text="大视图文本通知" 
  23. android:onClick="click" 
  24. /> 
  25. <Button  
  26. android:id="@+id/btn_03" 
  27. android:layout_width="match_parent" 
  28. android:layout_height="wrap_content" 
  29. android:text="大视图图片通知" 
  30. android:onClick="click" 
  31. /> 
  32. <Button  
  33. android:id="@+id/btn_04" 
  34. android:layout_width="match_parent" 
  35. android:layout_height="wrap_content" 
  36. android:text="进度条通知" 
  37. android:onClick="click" 
  38. /> 
  39. </LinearLayout> 

Android中通过Notification&NotificationManager实现消息通知

4、MainActivity中的代码:

 

 
  1. package com.xin.day__notificationdemo; 
  2. import java.util.Timer; 
  3. import java.util.TimerTask; 
  4. import android.app.Activity; 
  5. import android.app.Notification; 
  6. import android.app.NotificationManager; 
  7. import android.app.PendingIntent; 
  8. import android.content.Intent; 
  9. import android.graphics.BitmapFactory; 
  10. import android.os.Bundle; 
  11. import android.support.v.app.NotificationCompat; 
  12. import android.support.v.app.NotificationCompat.BigPictureStyle; 
  13. import android.support.v.app.NotificationCompat.BigTextStyle; 
  14. import android.support.v.app.NotificationCompat.Builder; 
  15. import android.util.Log; 
  16. import android.view.View; 
  17. public class MainActivity extends Activity { 
  18. //通知的唯一标识,在一个应用程序中不同的通知要区别开来 
  19. private static final int NO = x; 
  20. private static final int NO = x; 
  21. private static final int NO = x; 
  22. private static final int NO = x; 
  23. //进度条要用 
  24. private int progress = ; 
  25. @Override 
  26. protected void onCreate(Bundle savedInstanceState) { 
  27. super.onCreate(savedInstanceState); 
  28. setContentView(R.layout.activity_main); 
  29. //click方法,和xml文件中的各个按钮的onClick属性的值要一致 
  30. public void click(View view) { 
  31. //创建NotificationManager 
  32. final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
  33. //用switch语句控制四个控件 
  34. switch (view.getId()) { 
  35. case R.id.btn_: { 
  36. Notification notification = new Notification(); 
  37. notification.icon = R.drawable.ic_launcher; 
  38. notification.tickerText = "有消息了。。。"
  39. Intent intent = new Intent(this, MainActivity.class); 
  40. PendingIntent pendingIntent = PendingIntent.getActivity(this, , 
  41. intent, PendingIntent.FLAG_UPDATE_CURRENT); 
  42. notification.setLatestEventInfo(this".以前的通知""试试而已", pendingIntent); 
  43. notification.when = System.currentTimeMillis(); 
  44. notification.defaults = Notification.DEFAULT_ALL; 
  45. notification.flags = Notification.FLAG_AUTO_CANCEL; 
  46. notification.number = ; 
  47. notification.vibrate = new long[]{, }; 
  48. manager.notify(NO, notification); 
  49. break
  50. case R.id.btn_:{ 
  51. //大视图文本通知 
  52. //创建消息构造器,在扩展包 
  53. NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
  54. //设置当有消息是的提示,图标和提示文字 
  55. builder.setSmallIcon(R.drawable.ic_launcher).setTicker("有新消息了"); 
  56. //需要样式 
  57. BigTextStyle style = new BigTextStyle(); 
  58. style.setBigContentTitle("上课通知");//通知的标题 
  59. style.bigText("今天下午要在综B上jsp");//通知的文本内容 
  60. //大视图文本具体内容 
  61. style.setSummaryText("这是正常的课程安排,请各位同学按时上课"); 
  62. builder.setStyle(style); 
  63. //显示消息到达的时间,这里设置当前时间 
  64. builder.setWhen(System.currentTimeMillis()); 
  65. //获取一个通知对象 
  66. Notification notification = builder.build(); 
  67. notification.flags = Notification.FLAG_AUTO_CANCEL; 
  68. //发送(显示)通知 
  69. //notify()第一个参数id An identifier for this notification unique within your application 
  70. //get?意思说,这个通知在你的应用程序中唯一的标识符 
  71. manager.notify(NO, notification); 
  72. break
  73. case R.id.btn_:{ 
  74. //大视图图片通知 
  75. NotificationCompat.Builder builderPic = new Builder(this); 
  76. builderPic.setSmallIcon(R.drawable.ic_launcher).setTicker("新浪体育提醒"); 
  77. //进行设置 
  78. BigPictureStyle pictureStyle = new BigPictureStyle(); 
  79. pictureStyle.setBigContentTitle("新浪体育 快船VS骑士 "); 
  80. pictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_game)); 
  81. pictureStyle.setSummaryText(" 快船VS骑士 天王山之战!!!");//不要在意文字 
  82. //设置样式 
  83. builderPic.setStyle(pictureStyle); 
  84. //设置显示的时间 
  85. builderPic.setWhen(System.currentTimeMillis()); 
  86. Notification notification = pictureStyle.build(); 
  87. notification.flags = Notification.FLAG_AUTO_CANCEL; 
  88. // 
  89. manager.notify(NO, notification); 
  90. break
  91. case R.id.btn_:{ 
  92. //进度条通知 
  93. final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(this); 
  94. builderProgress.setSmallIcon(R.drawable.ic_launcher).setTicker("进度条通知"); 
  95. builderProgress.setProgress(, progress, false); 
  96. final Notification notification = builderProgress.build(); 
  97. //发送一个通知 
  98. manager.notify(NO, notification); 
  99. //创建一个计时器 
  100. Timer timer = new Timer(); 
  101. timer.schedule(new TimerTask(){ 
  102. @Override 
  103. public void run() { 
  104. Log.i("progress",progress+""); 
  105. while(progress <= ){ 
  106. progress ++; 
  107. try { 
  108. Thread.sleep(); 
  109. catch (InterruptedException e) { 
  110. // TODO Auto-generated catch block 
  111. e.printStackTrace(); 
  112. //更新进度条 
  113. builderProgress.setProgress(, progress, false); 
  114. //再次通知 
  115. manager.notify(NO, builderProgress.build()); 
  116. //计时器退出 
  117. this.cancel(); 
  118. //进度条退出 
  119. manager.cancel(NO); 
  120. return;//结束方法 
  121. }, ); 
  122. break
  123. default
  124. break

5、运行:我的虚拟机版本是4.0的(api19),按住通知左(右)滑动就可以让通知小时了。

效果如下:

Android中通过Notification&NotificationManager实现消息通知

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表