StatusBar的使用_例子讲解

Notification是一般是在Service这种后台服务中发起的,当后台服务要通知用户某一事项时,就发起一个Notification,然后通过这个Notification启动相应的Activity,而Service自己不应该启动Activity.


生成Notification要用到两个类,一个是Notification,两一个是NotificationManager.其实不建议通过Notification的构造函数创建Notification,而是用Notification.Builder来帮助生成。

生成一个Notification的步骤:

1. 得到一个NotificationManger的引用。这个要通过getSystemService(NOTIFICATION_SERVICE)方法来得到.

2. 生成一个Notification对象:

3. 定义Notification的内容和PendingIntent

4. 将Notification传递给NotificationManager


 

以下为程序例子:在MainActivity中有一个按钮,当点击按钮后会产生一个Notification在status bar中,然后点击打开这个Notification会打开ActionNotification这个Activity.

MainActivity.java:

 1 public class MainActivity extends Activity
 2 {
 3     /** Called when the activity is first created. */
 4     @Override
 5     public void onCreate(Bundle savedInstanceState)
 6     {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.main);
 9 
10         Button button = (Button) findViewById(R.id.btn_notice);
11         button.setOnClickListener(new OnClickListener()
12         {
13 
14             public void onClick(View v)
15             {
16                 setNotification();
17             }
18         });
19     }
20 
21     private void setNotification()
22     {
23         // 首先得到NotificationManager,通过getSystemService()方法得到
24         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
25         // 生成一个Notification,其实不建议直接采用构造方法,而是用Notification.Builder类生成Notification
26         Notification notice = new Notification(R.drawable.close2, "have downloaded", System.currentTimeMillis());
27         // 定义Notification的信息和PendingIntent
28         Intent intent = new Intent(this, ActionNotification.class);
29         PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
30         notice.setLatestEventInfo(this, "下载完成", "请打开查看", contentIntent);
31         // 将Notification传递给NotificationManager
32         nm.notify(1, notice);
33     }
34 }

ActionNotification.java:

 1 public class ActionNotification extends Activity
 2 {
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState)
 5     {
 6         super.onCreate(savedInstanceState);
 7         TextView textView = new TextView(this);
 8         textView.setText("歌曲 一千年以后 下载完成");
 9         LinearLayout layout = new LinearLayout(this);
10         layout.addView(textView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
11 
12         setContentView(layout);
13     }
14 }

 

程序运行结果:

                  

 


 为Notification增加声音提醒:

增加系统默认声音:

notification.defaults  |= Notification.DEFAULT_SOUND;

增加自定义声音:

notification.sound = Uri.parse("file:///sdcard/notification/ring.mp3");


 

 

为Notification增加震动提醒:

增加系统默认震动:

notification.defaults  |= Notification.DEFAULT_VIBRATE;

增加自定义震动:

long[] vibrate = {0. 100. 200, 300};

notification.vibrate = vibrate;

如果需要使用震动功能,需要加入权限:<uses-permission android:name="android.permission.VIBRATE" />


 

为Notification加入灯闪提醒:

增加系统默认灯闪:

notification.defaults  |= Notification.DEFAULT_LIGTHS;

增加自定义灯闪:

notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;


 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/hanyuan/archive/2012/04/12/StatusBar-Notifacation.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值