http://blog.csdn.net/a600423444/article/details/7421464
Android提供了三种通知类型方式:Toast Notifications、Status Bar Notification、Dialog Notification
现在分别来看看它们适用的场景与使用方法。
一、Toast Notifications
以背景改变方式,提示一些简短的消息,消息窗口自动淡入淡出,不接受交互事件。
例如:当下载某个文件完成时,可以提示简短的“保存成功”。
显示效果:
创建弹出提示方法:
1、创建Toast对象,可以通过Toast提供的静态方法makeText(Context context, String message, int duration)
context:应用上下文对象,这里可以传递getApplicationContext()
message:提示文本
duration:显示时长,可以使用Toast.LENGTH_SHORT、Toast.LENGTH_LONG
- Context context = getApplicationContext();
- Toast toast = Toast.makeText(context, "保存成功", Toast.LENGTH_LONG);
2、显示提示,调用show()方法
- toast.show();
上述两步也可简写为:
- Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show();
这样,最简单的提示信息已经完成。现在来看看如何创建自定义外观Toast notification。
3、自定义外观Toast通知
3.1、定义XML资源视图作为提示的外观
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/toast_layout_root"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="10dp"
- android:background="#DAAA"
- >
- <ImageView android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginRight="10dp"
- android:src="@drawable/icon"
- />
- <TextView android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:textColor="#FFF"
- />
- </LinearLayout>
其中TextView文本组件用来显示需要提示的文本。这里默认没有设置文字。
3.2、解析上述XML资源视图,并设置提示文本
- LayoutInflater inflater = getLayoutInflater();//XML资源布局填充对象
- View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
- //修改自定义布局中TextView文本,作为提示信息
- TextView textView = (TextView) layout.findViewById(R.id.text);
- textView.setText("自定义界面:保存成功");
3.3、创建Toast对象,并设置视图、显示视图
- Toast toast = new Toast(getApplicationContext());
- //设置垂直居中,水平、垂直偏移值为0,表示正中间。
- toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);//设置提示框位置,三个参数分别代表:对其方式、水平偏移值、垂直偏移值。
- toast.setDuration(Toast.LENGTH_LONG);
- toast.setView(layout);//设置显示的视图
- toast.show();
显示效果图:
更多关于Toast Notification的说明可以文档:http://android.toolib.net/guide/topics/ui/notifiers/toasts.html
二、Status Bar Notification
状态栏通知。当某个应用处于后台运行时需要提示用户某些信息时,不可能启动Activity。这时使用状态栏通知就非常合适。
例如:最经典的就是当接收到新短信时,可以在通知栏看到简要信息。
创建状态栏通知的过程:
1.取得通知管理器
- NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
2.实例化通知对象
- /**
- * new Notification(int icon, String message, long when)
- * 参数1:通知图标
- * 参数2:简短提示文本
- * 参数3:何时显示,这里使用的是时间戳
- */
- Notification notification = new Notification(R.drawable.icon, "状态栏通知测试", System.currentTimeMillis());
3.定义通知的详细信息、及PendIntent来设置激活的Activity
- //这里设置意图处理很简单,仅仅是当用户触摸详细信息时,将会显示MainActivity界面
- Intent notificationIntent = new Intent(this, MainActivity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 200, notificationIntent, 0);
- notification.setLatestEventInfo(this, "通知完整标题", "通知内容", pendingIntent);
4.传递到通知管理器,加入到通知队列
- manager.notify(11, notification);
这样,就完成了一个简单的状态栏通知。
除此之外,还可以设置通知的提示方式,如震动、音乐、闪烁等。
设置提示声音: