android开发之Notification学习笔记

举报
江南一点雨 发表于 2021/08/16 23:54:46 2021/08/16
【摘要】 今天总结了一下Notification的使用,与大家分享一下。 MainActivity.java: 本文参考:http://www.jb51.net/article/36567.htm,http://www.cnblogs.com/linjiqin/archive/2011/12/14/2288074.html public class MainActivity...

今天总结了一下Notification的使用,与大家分享一下。
MainActivity.java:

本文参考:http://www.jb51.net/article/36567.htmhttp://www.cnblogs.com/linjiqin/archive/2011/12/14/2288074.html

public class MainActivity extends Activity { private Button btn; private NotificationManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// showNotification();
// showNotification2();
// showNotification3();
// showNotification4();
// showNotification5(); showNotification6(); this.btn = (Button) this.findViewById(R.id.button1); this.btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { clearNotification(); } }); } /** * 自定义Notification布局 */ private void showNotification6() { RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification); Intent intent = new Intent(this, SecondActivity.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); //点击时给SecondActivity发送一条广播 remoteViews.setOnClickPendingIntent(R.id.paly_pause_music, pendingIntent); Intent intent1 = new Intent(this,SecondActivity.class); remoteViews.setOnClickPendingIntent(R.id.paly_pause_music, PendingIntent.getActivity(this, 0, intent1, 0)); NotificationCompat.Builder builder = new Builder(this); builder.setContent(remoteViews).setSmallIcon(R.drawable.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)).setOngoing(true) .setTicker("music is playing"); manager.notify(0, builder.build()); } /** * 大布局Picture类型notification * 这个方法貌似还有一点问题 */ private void showNotification5() { NotificationCompat.BigPictureStyle pictureStyle = new BigPictureStyle(); pictureStyle.setBigContentTitle("BigContentTitle") .setSummaryText("Summary Text").bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); Notification notification = new NotificationCompat.Builder(this) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) .setTicker("showBigView_pic").setContentInfo("ContentInfo") .setContentTitle("ContentTitle").setContentText("ContentText") .setStyle(pictureStyle) .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL) .build(); manager.notify(3, notification); } /** * 带进度条的通知栏 */ private void showNotification4() { final NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) .setSmallIcon(R.drawable.ic_launcher) .setTicker("showProgressBar") .setContentInfo("ContentInfo") .setOngoing(true) .setContentTitle("ContentTitle") .setContentText("ContentText"); new Thread(new Runnable() { @Override public void run() { int progress = 0; for(progress=0;progress<100;progress+=5){ //将第三个参数改为true,进度条会变为无明显进度的样式 builder.setProgress(100, progress, true); manager.notify(0, builder.build()); try { Thread.sleep(1000); } catch (Exception e) { } } //线程执行完毕之后,修改通知栏的显示 builder.setContentTitle("Download complete").setProgress(0, 0, false).setOngoing(false); manager.notify(0, builder.build()); } }).start(); } /** * 大通知栏 */ private void showNotification3() { NotificationCompat.BigTextStyle textStyle = new BigTextStyle(); textStyle.setBigContentTitle("BigContentTitle") .setSummaryText("Summary Text") .bigText("I'm Big Text......................................................................"); Notification notification = new NotificationCompat.Builder(this) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) .setSmallIcon(R.drawable.ic_launcher) .setTicker("Ticker") .setContentInfo("ContentInfo") .setContentTitle("ContentTitle") .setContentText("ContentText") .setStyle(textStyle) .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).build(); manager.notify(0, notification); } /** * 小通知栏 */ private void showNotification2() { Notification notification = new NotificationCompat.Builder(this) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) .setSmallIcon(R.drawable.ic_launcher) .setTicker("showNormal") .setContentInfo("ContextInfo") .setContentTitle("ContentTitle") .setContentText("ContentText") .setNumber(45) .setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) .build(); Intent notificationIntent =new Intent(MainActivity.this, SecondActivity.class); PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.contentIntent=contentItent; manager.notify(0, notification); } /** * 在状态栏显示通知 */ private void showNotification(){ // 创建一个NotificationManager的引用 NotificationManager notificationManager = (NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE); // 定义Notification的各种属性 Notification notification =new Notification(R.drawable.ic_launcher, "系统测试", System.currentTimeMillis()); //FLAG_AUTO_CANCEL   该通知能被状态栏的清除按钮给清除掉 //FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉 //FLAG_ONGOING_EVENT 通知放置在正在运行 //FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
// notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中   
// notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用    notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.flags |= Notification.FLAG_SHOW_LIGHTS; //DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等 //DEFAULT_LIGHTS  使用默认闪光提示 //DEFAULT_SOUNDS  使用默认提示声音 //DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE" />权限 notification.defaults = Notification.DEFAULT_LIGHTS; //叠加效果常量 //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND; notification.ledARGB = Color.BLUE; notification.ledOnMS =5000; //闪光时间,毫秒 // 设置通知的事件消息    CharSequence contentTitle ="系统消息"; // 通知栏标题    CharSequence contentText ="系统正在运行...."; // 通知栏内容    Intent notificationIntent =new Intent(MainActivity.this, SecondActivity.class); // 点击该通知后要跳转的Activity    PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, contentTitle, contentText, contentItent); // 把Notification传递给NotificationManager    notificationManager.notify(0, notification); } //删除通知  private void clearNotification(){ // 启动后删除之前我们定义的通知    NotificationManager notificationManager = (NotificationManager) this .getSystemService(NOTIFICATION_SERVICE); notificationManager.cancel(0); }
}
  
 
  • 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
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181

关键代码文中已有注释,完整代码下载。如有错误之后欢迎大家一起讨论。

文章来源: wangsong.blog.csdn.net,作者:_江南一点雨,版权归原作者所有,如需转载,请联系作者。

原文链接:wangsong.blog.csdn.net/article/details/46971929

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。