通知
两种通知方式:
1、 Toast
一、Toast
/**
* Toast 事例
* API文档中写道:
* Note: Do not use the public constructor for a Toast unless you are going to define the layout with setView(View).
* If you do not have a custom layout to use, you must use makeText(Context,int, int) to create the Toast.
* 翻译:不要使用Toast的构造函数创建对象,除非你准备用setView定义布局
* 如果你没有一个自定义的布局使用,你必须用makeTest(Context, int,int)创建一个Toast
*/
privatevoid toastTest() {
Toast toast = Toast.makeText(this,"test", Toast.LENGTH_LONG);
// Toast toast = new Toast(this); //这样是错的
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.show();
}
/**
* 自定义Toast 事例
* API文档中写道:
* Note: Do not use the public constructor for a Toast unless you are going to define the layout with setView(View).
* If you do not have a custom layout to use, you must use makeText(Context,int, int) to create the Toast.
* 翻译:不要使用Toast的构造函数创建对象,除非你准备用setView定义布局
* 如果你没有一个自定义的布局使用,你必须用makeTest(Context, int,int)创建一个Toast
*/
privatevoid customToastTest() {
//用getLayoutInflater()或者(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)得到LayoutInflater对象,如下面两行
// LayoutInflater li = getLayoutInflater();
LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = li.inflate(R.layout.toast_layout,null);
Toast toast = new Toast(this);
// Toast toast = Toast.makeText(this, "test", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
二、notification
privatevoid notificationTest() {
// 1、得到一个NotivicationManager对象
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 2、实例化Notification
int icon = R.drawable.ic_launcher;
CharSequence tickerText ="Hello";
long when = System.currentTimeMillis();
// Notification notification = new Notification();
Notification.Builder nb =new Notification.Builder(this);
nb.setSmallIcon(icon);
nb.setTicker(tickerText);
nb.setWhen(when);
// Notification noti = nb.getNotification();
//定义notification的消息和PendingIntent
nb.setContentText("Hello world!");
nb.setContentTitle("My notification");
Intent notificationIntent =new Intent(this, IntentToActivity.class);
// Bundle b = new Bundle();
finalint HELLO_ID = 1;
// b.putInt("id", HELLO_ID);
// b.putSerializable("a", (Serializable)nm);
// notificationIntent.putExtra("aa", b);
PendingIntent pd = PendingIntent.getActivity(this, 0, notificationIntent, 0);
nb.setContentIntent(pd);
Notification noti = nb.getNotification();
//把 Notification传到NotificationManager中
nm.notify(HELLO_ID, noti);
// 由于调用notify后,通知栏中有通知了,但点击通知后,通知不消失
// 目前打算点击通知栏中的通知后让通知消失,于是做了一下操作
//下面两行是将通知的id和NotificationManager对象传到启动的
// activity(IntentToActivity.class,在此类中定义了两个变量:
// private static NotificationManager nm;和private static int id;,//并且提供了setter,getter方法对外暴力接口)中,然后在
// IntentToActivity.class中的oncreate方法中调用nm.cancel(id);用//来取消通知栏中的通知
IntentToActivity.setId(HELLO_ID);
IntentToActivity.setNm(nm);
}
注意:文档中写道,Notification notification = new Notification(icon, tickerText, when);和
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
都已经过时,建议用Notification.Builder