码迷,mamicode.com
首页 > 移动开发 > 详细

小记:安卓发送通知栏通知

时间:2016-02-17 18:46:18      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

 1 package com.lixu.tongzhi;
 2 
 3 import android.app.Activity;
 4 import android.app.Notification;
 5 import android.app.NotificationManager;
 6 import android.app.PendingIntent;
 7 import android.content.Intent;
 8 import android.os.Bundle;
 9 import android.support.v4.app.NotificationCompat;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.widget.Button;
13 import android.widget.RemoteViews;
14 import android.widget.Toast;
15 
16 public class MainActivity extends Activity {
17 
18     private static final int ID = 1987;
19     private static final int REQUEST_CODE = 123;
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25 
26         Button btn1 = (Button) findViewById(R.id.fasong);
27         Button btn2 = (Button) findViewById(R.id.qingchu);
28 
29         btn1.setOnClickListener(new OnClickListener() {
30 
31             @Override
32             public void onClick(View v) {
33                 send();
34 
35                 Toast.makeText(getApplicationContext(), "发送通知成功!", 0).show();
36 
37             }
38         });
39 
40         btn2.setOnClickListener(new OnClickListener() {
41 
42             @Override
43             public void onClick(View v) {
44 
45                 delete();
46 
47                 Toast.makeText(getApplicationContext(), "清除通知成功!", 0).show();
48 
49             }
50         });
51 
52     }
53 
54     private void send() {
55         // 获取通知管理器
56         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
57 
58         NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
59         // 设置通知栏图片
60         mBuilder.setSmallIcon(R.drawable.sdfdf);
61 
62         Notification notification = mBuilder.build();
63         // RemoteViews中自定义Notification布局
64 
65         RemoteViews cv = new RemoteViews(getApplicationContext().getPackageName(), R.layout.list);
66         // 设置下拉后通知栏图片
67         cv.setImageViewResource(R.id.image, R.drawable.meimei);
68         // 设置内容
69         cv.setTextViewText(R.id.tv1, "我是小超人");
70         cv.setTextViewText(R.id.tv2, "我是小超人,主人有什么事情要吩咐。");
71         notification.contentView = cv;
72 
73         // 设置通知声音或者震动或闪光。
74         notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
75         // 通知时间
76         notification.when = System.currentTimeMillis();
77 
78         // 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
79         Intent intent = new Intent(getApplicationContext(), MainActivity.class);
80         // 当用户点击通知栏的Notification时候,切换回MainActivity。
81         PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), REQUEST_CODE, intent,
82                 PendingIntent.FLAG_UPDATE_CURRENT);
83         notification.contentIntent = pi;
84 
85         // 通过特定id来发送这个通知
86         manager.notify(ID, notification);
87 
88     }
89 
90     private void delete() {
91         // 获取通知管理器
92         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
93         manager.cancel(ID);
94 
95     }
96 
97 }

 

小记:安卓发送通知栏通知

标签:

原文地址:http://www.cnblogs.com/labixiaoxin/p/5196148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!