多任务机制下,程序跑在后台,用户使用服务,形成了一个良好的使用结构。程序运行的状态或结果,在不干扰用户当前使用的状态下,以通知的方式通知用户,能大大提升地用户的使用感。
在android下,与通知功能相关的组件大致有以下三大部分:
android.app.PendingIntent; android.app.NotificationManager; android.app.Notification;
NotificationManager可以通过系统的服务获取,我就是通过它向用户发出通知的
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification,通知的主体,通过设置其各种属性,来实现通知的效果,
PendingIntent,Intent的封装类,主要是用来导向点击通知后要启动的Activity。不过这里主要牵涉到Intent属性的设置,以下两段代码,前者跳转Activity时会新建一个Activity的实例,后者则会先从栈堆里弹出
intent = new Intent(NotificationTest.this, NotificationTest.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); pendIntent = PendingIntent.getActivity(NotificationTest.this, 0, intent, 0); notif.contentIntent = pendIntent;
intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setClass(NotificationTest.this, NotificationTest.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); pendIntent = PendingIntent.getActivity(NotificationTest.this, 0, intent, 0);
android下的通知组件使用起来很简单,下面是自己的一个例子,例子模拟下载的通知,并使用Timer和Handle一步更新通知上的进度条:
public class NotificationTest extends Activity { private NotificationManager notificationManager; private String svcName = Context.NOTIFICATION_SERVICE; private Intent intent; private PendingIntent pendIntent; private Notification notif; //通知的变量 private RemoteViews rv = null; //自定义通知的外观 private final int NOFI_REF = 1; //通知的标志 private TextView tv = null; private Timer timer = null; private int number = 0; private int num = 0; private boolean isOver = true; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.myTv); tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if(number!=0){ tv.setText("down again! \n"); } if (timer == null && isOver==true) { setNotification(); timer = new Timer(); timer.schedule(new MyTask(), 1000); isOver = false; } } }); tv.setText("Tis is Notification Test! \n Click screen to start! \n"); //获取系统的通知服务管理器实例 notificationManager = (NotificationManager) getSystemService(svcName); } private void setNotification() { long when = System.currentTimeMillis(); notif = new Notification(android.R.drawable.ic_media_rew, "Test Notification", when); //new一个通知的实例,并设置在状态栏的显示方式 // 点击通知时转移的内容 intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setClass(NotificationTest.this, NotificationTest.class); //设置intent的标志,以控制目标Activity的启动方式 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); pendIntent = PendingIntent.getActivity(NotificationTest.this, 0, intent, 0); notif.contentIntent = pendIntent; // //notif.defaults = Notification.DEFAULT_SOUND; //设置通知的声音,也可以通过uri属性自定义 long[] ss = {1000,500,1000}; notif.vibrate = ss; //设置通知时震动 rv = new RemoteViews(this.getPackageName(), R.layout.my_notification_view); //设置通知自定义的外观 notif.contentView = rv; // //设置进度条的最大进度和初始进度 notif.contentView.setProgressBar(R.id.pb, 100, 10, false); //显示下载的内容 notif.contentView.setTextViewText(R.id.tv_name, "正在下载"); //显示下载的进度 notif.contentView.setTextViewText(R.id.tv_p, "0%"); notif.flags = Notification.FLAG_ONGOING_EVENT; //设置通知的标志,定义在通知栏上的显示方式 notificationManager.notify(NOFI_REF, notif); //向系统发出通知 } class MyTask extends TimerTask { @Override public void run() { Message msg = new Message(); num+=5; Bundle b = new Bundle(); b.putInt("pb", num); msg.setData(b); msg.what = 1; handeler.sendMessage(msg); } }; private Handler handeler = new Handler(){ public void handleMessage(Message msg) { if(msg.what==1){ Bundle b =msg.getData(); int i = b.getInt("pb"); if(i<100){ tv.setText(tv.getText() + "Notification update:" + i + "\n"); timer.cancel(); timer = null; if(i%10==0){ rv.setProgressBar(R.id.pb, 100, i, false); //更新进度条 rv.setTextViewText(R.id.tv_p, i+"%"); //更新label的值 notif.vibrate = null; //关掉震动 notif.defaults = 0; //关掉声音 notificationManager.notify(NOFI_REF, notif); //发出通知,更新通知栏上的状态 } timer = new Timer(); timer.schedule(new MyTask(), 500); } else { timer.cancel(); timer = null; //notif.contentView.removeAllViews(R.id.progress_lay); //去掉进度条 //notif.contentView.setTextViewText(R.id.tv_name, "下载完成"); //更新显示的信息 notif.setLatestEventInfo(NotificationTest.this, "下载完成", "OK!", pendIntent); //如方法名称一样,设置要通知的最新事件信息 //要注意的是,绑定通知的contentView和通知的setLatestEventInfo方法, //两者只能选一种,否则通知只会展现最后绑定的效果 notif.flags = Notification.FLAG_AUTO_CANCEL; //设置通知在用户点击后就会自动取消 long[] ss = {1000,500,1000}; notif.vibrate = ss; notif.number = ++number; //设置通知的条数 notificationManager.notify(NOFI_REF, notif); //发出通知,更新通知栏上的状态 isOver = true; num = 0; } } } }; }