Toast和Notification
针对于Dialog来说是更友好,轻量级的机制来提醒用户。不会打断用户
1 Notification
通过这种方式你可以使系统:将一个图标在状态条上闪,让机器震动,发出声音等。
2 Toast
这是一个无模式的小窗体,会将显示的信息显示在首页面中:
创建和配置新的Notification需要经历三步
(1)创建一个新的Notification对象 //图标、文字和Notification的当前时间
(2)使用setLatestEventInfo方法来配置Notification在扩展的状态窗口中的外观。
setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)
context:指显示在哪里(如果是本Activity使用this);
contentTitle:指展开本Notification后显示的标题
contentText:指展开本Notification后显示的内容
contentIntent:指The intent to execute when the expanded status entry is clicked
获取一个PendingIntent:PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
(3)触发一个Notification
int notificationRef = 1;
notificationManager.notify(notificationRef, notification);
//其中notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
protected void showNotification() {
Notification nt=new Notification(R.drawable.icon,"标题",System.currentTimeMillis());
PendingIntent pin=PendingIntent.getActivity(this,0,new Intent(),0);
nt.setLatestEventInfo(this, "展开后的标题", "展开后的内容", pin);
manager.notify(1, nt);
}
-------------------------------------------------------------------------------------------------------------------------
2 toast
new Toast(Context);
setView(View);
setDuration(); //Toast.LENGTH_LONG;Toast.LENGTH_SHORT
show() //方法
3 设置权限:在AndroidManifest.xml里设置权限:
<uses-permission android:name="android.permission.VIBRATE" />