Andorid通知示例(二)

复制代码
 1 public class LaunchNotificationActivity
2 extends Activity {
3 /** Called when the activity is first created. */
4 @Override
5 public void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7
8 LinearLayout layout = new LinearLayout(this);
9 layout.setOrientation(LinearLayout.VERTICAL);
10
11 TextView textView = new TextView(this);
12 textView.setText("演示生成通知。");
13
14 Button button = new Button(this);
15 button.setText("通知演示");
16 button.setOnClickListener(new OnClickListener() {
17 @Override
18 public void onClick(View v) {
19 NotificationManager mNotificationManager =
20 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
21 int icon = R.drawable.smalllogo;
22 long when = System.currentTimeMillis();
23 Notification notification = new Notification(icon, "掌中彩温馨提示", when);// 第一个参数为图标,第二个参数为标题,第三个为通知时间
24 notification.defaults = Notification.DEFAULT_SOUND;// 发出默认声音
25 // Intent openintent = new Intent(this, OtherActivity.class);
26 PendingIntent contentIntent =
27 PendingIntent.getActivity(LaunchNotificationActivity.this, 0, null, 0);// 当点击消息时就会向系统发送openintent意图
28 notification.setLatestEventInfo(LaunchNotificationActivity.this, "标题",
29 "亲~您已经一周没打XXX了,随机一注试试手气吧", contentIntent);
30 notification.contentIntent =
31 PendingIntent.getActivity(LaunchNotificationActivity.this, 0,
32 new Intent(LaunchNotificationActivity.this,
33 LaunchNotificationActivity.class), 0);
34 notification.flags |= Notification.FLAG_AUTO_CANCEL;// 选择后不再在通知栏显示
35 notification.defaults |= Notification.DEFAULT_SOUND;
36 mNotificationManager.notify(0, notification);// 发送通知
37 }
38 });
39
40 layout.addView(textView);
41 layout.addView(button);
42 this.setContentView(layout);
43 }
44 }
复制代码
posted @   灰太狼_lilongmin  阅读(332)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示