Android常用的反馈系统状态信息的方式主要有三种

  1. Toast提醒
  2. 通知栏提醒
  3. 对话框提醒

三种提醒分别适用于页面的提示、系统交互事件的通知和非常重要的提醒;

一、Toast

  1. Toast toast = Toast.makeText(MainActivity.this, "Toast 通知", Toast.LENGTH_SHORT);
  2. toast.show();

Toast提醒最最简单方便的,默认情况下Toast显示在Activity下文水平居中,不过也可以通过代码设置为显示在其他位置

  1. toast.setGravity(Gravity.CENTER, 0, 0);

如果普通的文本无法满足要求,还可以设置自定义的View

  1. toast.setView(view);

二、Notification
  Notification更适用于交互事件的通知,常用于短信、即时消息、下载、外围设备的状态变化场景,支持文字显示、振动、三色灯、振铃音等多种提示形式,默认情况下,Notification只显示消息标题\消息内容\时间三项;

Android提供了一个Notification管理器NotificationManager,我们可以通过NotificationManager来控制一个Notification的显示与消除;

1。快速显示Notification

  1. public void showNotification() {
  2. NotificationManager notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  3. Intent intent = new Intent(LoginActivity.this, LoginActivity.class);
  4. PendingIntent contentIntent = PendingIntent.getActivity(
  5. LoginActivity.this, 0, intent, 0); // contentIntent
  6. Notification noti = new Notification();
  7. noti.icon = R.drawable.ic_launcher; // 通知的图标
  8. noti.tickerText = "tickerText"; // 在通知显示前,在手机状态栏会有一个快速的通知,
  9. noti.flags = Notification.FLAG_AUTO_CANCEL;// 设置通知被点击后自动取消
  10. noti.setLatestEventInfo(this, "Title", "Notification Content",
  11. contentIntent);
  12. // 指定notification的Context对象,标题,内容,以及点击对应的contentIntent
  13. notiManager.notify(12, noti);
  14. }

2。上面的代码快速的显示了一个Notification,我们还可以对Notification进行一些个性化的设定,比如指定提示音,震动方式等;

  1. noti.defaults = Notification.DEFAULT_SOUND;// 使用默认的提示声音
  2. noti.defaults |= Notification.DEFAULT_VIBRATE;// 添加默认震动方式

除了使用默认的震动方式,还可以自定义震动方式,自定义提示声音等;

>>1.自定义振动方式:

  1. Notification noti = new Notification();
  2. noti.vibrate = new long[] { 0, 700, 500, 1000 }; // 延迟0秒,振动0.7秒,延迟0.5秒,再振动1秒

>>2.三色灯

  1. noti.flags |= Notification.FLAG_SHOW_LIGHTS; // 设置标志位为FLAG_SHOW_LIGHTS之后才能显示三色灯,如果设置为默认的话:DEFAULTS_LIGHTS;
  2. noti.ledARGB = Color.parseColor("#00000000");
  3. noti.ledOffMS = 300;
  4. noti.ledOnMS = 300;

>>3.铃声提醒

  1. noti.sound = Uri.parse("filepath");
  2. noti.defaults |= Notification.DEFAULT_SOUND; // 默认铃声

>>4.提醒标志位

  1. FLAG_SHOW_LIGHTS //三色灯
  2. FLAG_ONGOING_EVENT //发起事件
  3. FLAG_INSISTENT //铃声将持续到notification取消或notificaton窗口打开
  4. FLAG_ONLT_ALERT_ONCE //发起notification后,铃声或振动只执行一次
  5. FLAG_AUTO_CALCEL //单击后自动消失
  6. FLAG_NO_CLEAR //只有全部清除时,notification都会被清除
  7. FLAG_FOREGROUND_SERVICE //表示正在运行的服务

>>5.针对意图的包装对象,为了使Notification能够响应点击事件,需要设置notification的contentIntent对象

  1. Intent intent = new Intent(this.getApplicationContext(),this.getClass());
  2. // 设置Intent.FLAG_ACTIVITY_NEW_TASK
  3. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  4. PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); // 生成PendingIntent对象

3。清除Notification

清除Notification有两种方式

>>1.通过NotificationManager来清除

  1. notiManager.cancel(12); // 传入Notification显示时的id

>>2.通过Intent来清除

在执行清除Notification操作时,通过设计notification的deleteIntent变量可以响应这个事件;

  1. Notification noti = new Notification();
  2. Intent deleteIntent = new Intent();
  3. deleteIntent.setClass(LoginActivity.this, this.getClass());
  4. deleteIntent.setAction(Intent.ACTION_DELETE);
  5. noti.deleteIntent = PendingIntent.getBroadcast(
  6. getApplicationContext(), 0, deleteIntent, 0);
  7. notiManger.notify(12, noti); // 同样需要传入Notification显示时的id

为了响应紧急事件(如来电),需要设置Notification的fullScreenIntent变量

  1. Notification noti = new Notification();
  2. Intent deleteIntent = new Intent();
  3. deleteIntent.setClass(getApplicationContext(), null);
  4. deleteIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  5. noti.fullScreenIntent = PendingIntent.getActivity(getApplicationContext(), 0, deleteIntent, 0);

4。上面所说的是最简单的Notification,有时候我们会看到一些各式各样布局的Notification,它其实是通过RemoteViews来实现的;下面是模仿下载文件的一个例子;

>>1.定义一个layout,里面定义了notification的显示界面;

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="64dp"
  5. android:orientation="horizontal" >
  6.  
  7. <ImageView
  8. android:id="@+id/noti_iv"
  9. android:layout_width="64dp"
  10. android:layout_height="match_parent"
  11. android:scaleType="center" />
  12.  
  13. <ProgressBar
  14. android:id="@+id/noti_pb"
  15. style="?android:attr/progressBarStyleHorizontal"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_gravity="center_vertical"
  19. android:layout_marginLeft="20.0dip"
  20. android:layout_marginRight="20.0dip" />
  21.  
  22. </LinearLayout>

在这个xml文件中,定义了一个ImageView和ProgressBar,分别用来显示notification的图标和下载进度;

>>2.编写Java代码实现

  1. public class LoginActivity extends Activity {
  2. String tag = "LoginActivity";
  3. private int progress = 0;
  4. private Notification noti;
  5. private NotificationManager notiManger;
  6. private Handler handler;
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_logion);
  12. notiManger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  13. handler = new Handler() {
  14. @Override
  15. public void handleMessage(Message msg) {
  16. progress += 5;
  17. getNotification(LoginActivity.this, progress);
  18. notiManger.notify(1024, noti);
  19. if (progress == 100) {
  20. notiManger.cancel(1024);
  21. return;
  22. }
  23. sendEmptyMessageDelayed(0, 1000);
  24. }
  25. };handler.sendEmptyMessageDelayed(0, 2000);
  26. }
  27.  
  28.    private Notification getNotification(Context context, int progress) {
  29. if (noti == null) {
  30. noti = new Notification();
  31. noti.icon = R.drawable.ic_launcher;
  32. noti.tickerText = "正在下载新版本...";
  33. }
  34. noti.flags = Notification.FLAG_NO_CLEAR; // 设置Notification为不可清除
  35.  
  36. // 指定个性化视图
  37. RemoteViews rv = new RemoteViews(context.getPackageName(),
  38. R.layout.app_update_noti);
  39. rv.setImageViewResource(R.id.noti_iv, R.drawable.ic_launcher); // 设置icon
  40. rv.setProgressBar(R.id.noti_pb, 100, progress, false); // 设置progressbar进度
  41. Intent intent = new Intent(context, MainActivity.class);
  42. PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
  43. intent, PendingIntent.FLAG_CANCEL_CURRENT);
  44. noti.contentView = rv;
  45. // 指定内容意图
  46. noti.contentIntent = contentIntent;
  47. return noti;
  48. }
  49. }

通过上面的代码,就可以看到一个在通知栏不断更新进度的通知;

主要有两点需要注意,一是添加一个为 Notification.FLAG_NO_CLEAR 的 flags ,二是指定notification#contentView为一个RemoteViews;

三、对话框提醒

Android 通知机制 Toast和Notification的更多相关文章

  1. Android应用程序组件Content Provider的共享数据更新通知机制分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6985171 在Android系统中,应用程序组 ...

  2. Android中的消息通知(NotificationManager和Notification)

    下面来谈谈notification,这个notification一般用在电话,短 信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这 ...

  3. Android 通知(Notification)

    1.介绍 2.常用属性 3.java后台代码 package com.lucky.test30notification; import android.app.Notification; import ...

  4. [iOS基础控件 - 6.10] Notification 通知机制

    A.定义      iOS程序都有一个NSNotificationCenter的单例对象,用来负责发布不同对象之间的通知      任何对象都能够在NSNotificationCenter发布通知,发 ...

  5. Android开发之漫漫长途 ⅥI——Android消息机制(Looper Handler MessageQueue Message)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  6. Android开发之漫漫长途 Ⅶ——Android消息机制(Looper Handler MessageQueue Message)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  7. Binder对象死亡通知机制

    本文參考<Android系统源码情景分析>,作者罗升阳. 一.Binder库(libbinder)代码:        ~/Android/frameworks/base/libs/bin ...

  8. Android随笔之——Android广播机制Broadcast详解

    在Android中,有一些操作完成以后,会发送广播,比如说发出一条短信,或打出一个电话,如果某个程序接收了这个广播,就会做相应的处理.这个广播跟我们传统意义中的电台广播有些相似之处.之所以叫做广播,就 ...

  9. Android广播机制的深入学习

    部分内容转载自http://www.cnblogs.com/lwbqqyumidi/p/4168017.html 1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者 ...

随机推荐

  1. 使用ListView+ObjectDataSource+DataPager实现增删改查加分页

    一.配置objectDataSource 选择业务逻辑层的类 二.配置Select对应的方法,必须是一个带两个整型参数的方法,第一个参数表示要查看的第一条记录的前一条30,第二个参数每页最多能显示的记 ...

  2. Qt-事件处理-鼠标事件

    根据书中的内容,简单的实现鼠标相关的内容 源代码如下 .h #ifndef MOUSEEVENT_H #define MOUSEEVENT_H #include <QMainWindow> ...

  3. selenium自动化之显式等待和EC(expected_conditions)模块

    很多人都有这种经历,selenium脚本当前运行没问题,过了一段时间再运行就报错了,然后过几天又好了.其中的原因估计60%的人都知道,是因为元素加载这块有问题.通常的解决方案就是加上sleep或者隐式 ...

  4. MySQL☞sign函数

    sign( )函数:判断数值的正负性,如果数值是正数,返回值是1,如果该数值是负数,返回值是-1,如果该数值是 0,返回值也是0. 格式: select sign(数值) from 表名 例子: 1. ...

  5. 第四十篇 Python之设计模式总结-简单工厂、工厂方法、抽象工厂、单例模式

    一. 简单工厂 简单工厂模式(Simple Factory Pattern):是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类. 简单工厂的用处不大,主要就是一个if... ...

  6. 使用gitlab时候 fork仓库不会实时从主仓库更新解决方案

    付费用户可以使用现成的方案,地址见 链接 但是私有gitlab时候,需要手动进行如下操作 1. Clone your fork: git clone git@github.com:YOUR-USERN ...

  7. Appium_Python_API说明

    Appium_Python_API 1.contexts contexts(self): Returns the contexts within the current session. 返回当前会话 ...

  8. 数据库Mysql的学习(四)-表的记录操作

    ,);//指定插入的顺序 ,);//按照默认的插入 ,),(,)(,);//同时插入多条数据 //将查询结果插入表中 CREATE TABLE TEXT( category_id INT PRIMAR ...

  9. Cortex-M3(NXP LPC 1788) 启动代码

    startup_LPC177x_8x.s启动代码分析. 参考资料: Cortex-M3 (NXP LPC1788)之启动代码分析 ARM启动过程(Cortex-M3 NXP LPC1768为例) ;/ ...

  10. Windows环境下的TensorFlow安装过程

    安装环境 Windows8.1 python3.5.x(TensorFlow only supports version 3.5.x of Python on Windows) pip 9.0.1 t ...