学习status bar notification

status bar notification 添加一个icon到系统的状态栏中(可选择是否有文本信息),添加一段信息到"Notifications"窗口中。当在后台运行的服务需要与用户进行交互时我们可以使用Status BarNotification。

如何创建一个Notification,必须知道两个类,一个是Notification和另外一个NotificationManager。从文档中可以看到创建一个Notification应该具备哪些要求

A status barnotification requires all of the following:
1、Anicon for the statusbar
2、Atitle and expandedmessage for the expanded view (unless you define a customexpanded view)
3、APendingIntent, to befired when the notification is selected
下面写一个程序来模拟一下Status barnotification 项目截图如下:

MainActivity.java代码如下:

packagecom.cooguo.statusbar.activity;
importcom.cooguo.statusbar.service.StatusService;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
       
       private Button btnStatusBar = null;
    @Override
    public void onCreate(BundlesavedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
        btnStatusBar=(Button)findViewById(R.id.btnStatusBar);
        btnStatusBar.setOnClickListener(listener);
    }
    private OnClickListener listener =new OnClickListener(){
             @Override
             public void onClick(View view) {
                    switch (view.getId()) {
                    case R.id.btnStatusBar:
                           Intent iten = new Intent(MainActivity.this,StatusService.class);
                           startService(iten);
                           break;
                    default:
                           break;
                    }
                     
             }
       
    };
       @Override
       protected void onStart() {
             NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
             notificationManager.cancel(R.layout.main);//R.layout.main作为notify的唯一ID进行获取
              super.onStart();
       }
}

StatusService.java代码如下:

package com.cooguo.statusbar.service;
importcom.cooguo.statusbar.activity.MainActivity;
import com.cooguo.statusbar.activity.R;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;

public class StatusService extends IntentService{
         //private static finalint KUKA = 0;
         private static finalString TAG= "StatusService";
         public StatusService(){
                  super("StatusService");
         }

         @Override
         protected voidonHandleIntent(Intent intent) {
       Log.i(TAG, "开始下载...");
      showNotification(false);
       try {
                  Thread.sleep(5000);
        } catch (InterruptedException e) {
                  e.printStackTrace();
        }
      showNotification(true);
       Log.i(TAG, "游戏下载完成。");
       
         }

         private voidshowNotification(boolean flag) {
            String info = "";
                   if(!flag){
                             info = "游戏开始下载.....";
                   }else{
                             info = "游戏开始完成。";
                   }
                 
                  Notification notification = newNotification(R.drawable.sicon, info, System.currentTimeMillis());
                  Intent intent = new Intent(this,MainActivity.class);
                  PendingIntent contentIntent =PendingIntent.getActivity(this, 0, intent , PendingIntent.FLAG_UPDATE_CURRENT);
                  notification.setLatestEventInfo(this, "CS游戏", "此游戏下载有奖哦,请点击!", contentIntent);
        NotificationManagernotificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
       notificationManager.notify(R.layout.main, notification);//用R.layout.main作为notify的唯一ID
         }

}

程序运行图:




拉下StatusBar如图:

点击CS游戏就会消失。

这样一个简单的功能也是很常用的功能,应该多参考开发文档。

另外文档中还提到可选状态

Optional settings for the status bar notification include:

  • A ticker-text message for the status bar
  • An alert sound
  • A vibrate setting
  • A flashing LED setting


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值