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

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

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

一、Toast

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

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

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

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

        toast.setView(view);

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

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

1。快速显示Notification

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

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

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

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

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

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

>>2.三色灯

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

>>3.铃声提醒

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

>>4.提醒标志位

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

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

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

3。清除Notification

清除Notification有两种方式

>>1.通过NotificationManager来清除

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

>>2.通过Intent来清除

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

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

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

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

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

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

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="64dp"    android:orientation="horizontal" >    <ImageView        android:id="@+id/noti_iv"        android:layout_width="64dp"        android:layout_height="match_parent"        android:scaleType="center" />    <ProgressBar        android:id="@+id/noti_pb"        style="?android:attr/progressBarStyleHorizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:layout_marginLeft="20.0dip"        android:layout_marginRight="20.0dip" /></LinearLayout>

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

>>2.编写Java代码实现

public class LoginActivity extends Activity {    String tag = "LoginActivity";    private int progress = 0;    private Notification noti;    private NotificationManager notiManger;    private Handler handler;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_logion);        notiManger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        handler = new Handler() {            @Override            public void handleMessage(Message msg) {                progress += 5;                getNotification(LoginActivity.this, progress);                notiManger.notify(1024, noti);                if (progress == 100) {                    notiManger.cancel(1024);                    return;                }                sendEmptyMessageDelayed(0, 1000);            }        };handler.sendEmptyMessageDelayed(0, 2000);    }

   private Notification getNotification(Context context, int progress) { if (noti == null) { noti = new Notification(); noti.icon = R.drawable.ic_launcher; noti.tickerText = "正在下载新版本..."; } noti.flags = Notification.FLAG_NO_CLEAR; // 设置Notification为不可清除 // 指定个性化视图 RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.app_update_noti); rv.setImageViewResource(R.id.noti_iv, R.drawable.ic_launcher); // 设置icon rv.setProgressBar(R.id.noti_pb, 100, progress, false); // 设置progressbar进度 Intent intent = new Intent(context, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); noti.contentView = rv; // 指定内容意图 noti.contentIntent = contentIntent; return noti; }}

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

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

三、对话框提醒

更多相关文章

  1. android后台下载服务的完成及事项
  2. Android联系人自动过滤并显示“新建联系人”
  3. Android通知栏颜色改变方法
  4. Android(安卓)中显示 Gif 格式图
  5. 【Android入门】Actionbar不显示overflow(右边三个小点)
  6. Mac系统中,Eclipse没有显示设备
  7. Android(安卓)Studio 关于高德地图的开发:显示地图
  8. Android隐藏输入法键盘(hideSoftInputFromInputMethod没有效果)(
  9. Android_开发片段(Part 3)

随机推荐

  1. Android基础之Android系统架构
  2. android下不规则多边形填充位图
  3. Android调用相机程序和图片处理程序获得
  4. 【Android】IPC(进程间通信)
  5. android:shape的使用
  6. android 使用Maven多项目搭建纪要
  7. android之计时器 chronometer
  8. 获取Android的Java源代码并在Eclipse中关
  9. android 上调试动态库方法
  10. android获得密钥