安卓开发笔记——Notification通知栏

当用户有没有接到的电话的时候,Android顶部状态栏里就会出现一个小图标。提示用户有没有处理的快讯,当拖动状态栏时,可以查看这些快讯。Android给我们提供了NotificationManager来管理这个状态栏。可以很轻松的完成。

很基础的东西,直接看注释就可以了,随手粘贴。

看下效果图:

  

 

 1 package com.example.notificationdemo;
 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.Context;
 8 import android.content.Intent;
 9 import android.os.Bundle;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.widget.Button;
13 import android.widget.Toast;
14 
15 public class MainActivity extends Activity implements OnClickListener {
16 
17     private Button send;
18     private Button cancel;
19 
20     private int flag=888;// 用来标志当前是哪个Notification
21     private NotificationManager mNotificationManager;
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main);
27         initView();
28         initListener();
29         // 取得通知系统服务
30         mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
31     }
32 
33     private void initListener() {
34         this.send.setOnClickListener(this);
35         this.cancel.setOnClickListener(this);
36     }
37 
38     private void initView() {
39         this.send = (Button) findViewById(R.id.send);
40         this.cancel = (Button) findViewById(R.id.cancel);
41     }
42 
43     @Override
44     public void onClick(View v) {
45         switch (v.getId()) {
46         case R.id.send:
47             // 发送通知
48             sendNotification();
49             break;
50         case R.id.cancel:
51             // 取消通知
52             mNotificationManager.cancel(flag);
53             break;
54         }
55     }
56 
57     private void sendNotification() {
58 
59         // 构建通知显示类
60         Notification.Builder builder = new Notification.Builder(this);
61         // 设置通知样式内容
62         builder.setSmallIcon(R.drawable.ic_launcher);// 设置顶部通知栏小图标
63         builder.setContentTitle("我是通知标题栏");// 设置具体标题
64         builder.setContentText("我是通知具体内容");// 设置具体内容
65         builder.setWhen(System.currentTimeMillis());// 设置时间
66         // 设置提示灯,震动,声音(可以一起设置DEFAULT_ALL,需要对应权限)
67         builder.setDefaults(Notification.DEFAULT_LIGHTS);
68         builder.setDefaults(Notification.DEFAULT_SOUND);
69         builder.setDefaults(Notification.DEFAULT_VIBRATE);
70 
71         Intent intent = new Intent(this, MainActivity.class);
72         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
73 
74         // 设置点击跳转Intent
75         builder.setContentIntent(pendingIntent);
76 
77         // 设置通知
78         Notification notification;
79         if (android.os.Build.VERSION.SDK_INT >= 16) {
80             notification = builder.build();// 系统4.1以上
81             Toast.makeText(this, "当前系统版本4.1以上", Toast.LENGTH_LONG).show();
82         } else {
83             notification = builder.getNotification();// 系统4.1以下
84             Toast.makeText(this, "当前系统版本4.1以下", Toast.LENGTH_LONG).show();
85         }
86 
87         // 发送通知
88         mNotificationManager.notify(flag, notification);
89     }
90 
91 }

 

posted @ 2015-07-14 13:19  李晨玮  阅读(705)  评论(0编辑  收藏  举报