NotificationManager 发送通知

该应用的界面如下,界面代码在此不再给出,源码github账户下载

MainActivity.java

 1 public class MainActivity extends Activity {
 2     private TextView tvTitle;
 3     private TextView tvContent;
 4     private Button btnSend;
 5     private String title;
 6     private String content;
 7     
 8     public void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         
12         tvTitle=(TextView) findViewById(R.id.editText1);
13         tvContent=(TextView) findViewById(R.id.EditText01);
14         btnSend=(Button) findViewById(R.id.btnSend);
15 
16         btnSend.setOnClickListener(new OnClickListener(){
17 
18             @Override
19             public void onClick(View v) {
20                 // TODO Auto-generated method stub
21                 send();
22             }
23         });
24         
25     }
26     public void send(){
27         title=tvTitle.getText().toString();//标题
28         content=tvContent.getText().toString();//内容
29         
30         //1.得到NotificationManager服务
31         NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
32         //2.实例化一个通知,指定图标、概要、时间
33         Notification n = new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());
34         //3.指定通知的标题、内容和intent
35         Intent intent = new Intent(this, MainActivity.class);
36         PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0);
37         n.setLatestEventInfo(this, title, content, pi);
38         //指定声音
39         //n.defaults = Notification.DEFAULT_SOUND; 
40         //4.发送通知
41         nm.notify(1, n);
42     }
43 
44 }

点击“发送”按钮,即可在通知栏显示发送的消息!

 本例GitHub源码下载

posted @ 2014-08-01 17:26  HuijunZhang  阅读(860)  评论(0编辑  收藏  举报
中国