多任务机制下,程序跑在后台,用户使用服务,形成了一个良好的使用结构。程序运行的状态或结果,在不干扰用户当前使用的状态下,以通知的方式通知用户,能大大提升地用户的使用感。

在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. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);tv = (TextView) findViewById(R.id.myTv);tv.setOnClickListener(new OnClickListener() {@Overridepublic 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 {@Overridepublic 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;}}}};}

更多相关文章

  1. Android获取常用辅助方法(获取屏幕高度、宽度、密度、通知栏高度
  2. Android(安卓)Design Library之四:BottomSheetDialog
  3. Android布局之二——Relative Layout
  4. 【攻克Android(安卓)(43)】WebView (网络视图)
  5. 为Activity屏幕的标题添加图标
  6. Android(安卓)样式 :shape selector 篇
  7. Android(安卓)CoordinatorLayout+AppBarLayout实现toolbar背景渐
  8. 怎样将Activity设置成窗口模式
  9. Bitmap recycle()

随机推荐

  1. Android(安卓)pm命令 以及在终端安装卸载
  2. Android自动测试之benchmark test 简介
  3. Android 性能优化之内存泄漏检测以及内存
  4. android 学习技术点 网址集锦
  5. 常用组件:android的图片组件ImageView
  6. Android Flash 10.1与Lite版视频播放能力
  7. Android 软键盘控制弹出(很好使,自己写的,绝
  8. Android 第一课——Android架构
  9. Android系统原理与开发要点详解
  10. android sdk manager 更新不到最新资源列