Android Notifications通知詳解
一、Toast Notifications
以背景改變方式,提示一些簡短的消息,消息窗口自動淡入淡出,不接受交互事件。
例如:當(dāng)下載某個文件完成時,可以提示簡短的“保存成功”。
顯示效果:
創(chuàng)建彈出提示方法:
1、創(chuàng)建Toast對象,可以通過Toast提供的靜態(tài)方法makeText(Context context, String message, int duration)
context:應(yīng)用上下文對象,這里可以傳遞getApplicationContext()
message:提示文本
duration:顯示時長,可以使用Toast.LENGTH_SHORT、Toast.LENGTH_LONG
- Context context = getApplicationContext();
 - Toast toast = Toast.makeText(context, "保存成功", Toast.LENGTH_LONG);
 
2、顯示提示,調(diào)用show()方法
- toast.show();
 
上述兩步也可簡寫為:
- Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show();
 
這樣,最簡單的提示信息已經(jīng)完成。現(xiàn)在來看看如何創(chuàng)建自定義外觀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文本組件用來顯示需要提示的文本。這里默認(rèn)沒有設(shè)置文字。
3.2、解析上述XML資源視圖,并設(shè)置提示文本
- 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、創(chuàng)建Toast對象,并設(shè)置視圖、顯示視圖
- Toast toast = new Toast(getApplicationContext());
 - //設(shè)置垂直居中,水平、垂直偏移值為0,表示正中間。
 - toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);//設(shè)置提示框位置,三個參數(shù)分別代表:對其方式、水平偏移值、垂直偏移值。
 - toast.setDuration(Toast.LENGTH_LONG);
 - toast.setView(layout);//設(shè)置顯示的視圖
 - toast.show();
 
顯示效果圖:
#p#
二、Status Bar Notification
狀態(tài)欄通知。當(dāng)某個應(yīng)用處于后臺運(yùn)行時需要提示用戶某些信息時,不可能啟動Activity。這時使用狀態(tài)欄通知就非常合適。
例如:最經(jīng)典的就是當(dāng)接收到新短信時,可以在通知欄看到簡要信息。
創(chuàng)建狀態(tài)欄通知的過程:
1.取得通知管理器
- NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
 
2.實(shí)例化通知對象
- /**
 - * new Notification(int icon, String message, long when)
 - * 參數(shù)1:通知圖標(biāo)
 - * 參數(shù)2:簡短提示文本
 - * 參數(shù)3:何時顯示,這里使用的是時間戳
 - */
 - Notification notification = new Notification(R.drawable.icon, "狀態(tài)欄通知測試", System.currentTimeMillis());
 
3.定義通知的詳細(xì)信息、及PendIntent來設(shè)置激活的Activity
- //這里設(shè)置意圖處理很簡單,僅僅是當(dāng)用戶觸摸詳細(xì)信息時,將會顯示MainActivity界面
 - Intent notificationIntent = new Intent(this, MainActivity.class);
 - PendingIntent pendingIntent = PendingIntent.getActivity(this, 200, notificationIntent, 0);
 - notification.setLatestEventInfo(this, "通知完整標(biāo)題", "通知內(nèi)容", pendingIntent);
 
4.傳遞到通知管理器,加入到通知隊列
- manager.notify(11, notification);
 
這樣,就完成了一個簡單的狀態(tài)欄通知。
除此之外,還可以設(shè)置通知的提示方式,如震動、音樂、閃爍等。
設(shè)置提示聲音:
- notification.sound = Uri.parse("file:///sdcard/On Call.mp3");
 
設(shè)置震動的交替模式:
- notification.vibrate = new long[]{0,100,200,300};
 
這里vibrate是一個長整型數(shù)組,用來設(shè)置震動交替時長,第一個值表示震動開始之前,第二個值表示第一次震動的時間,第三個值表示第二次震動的時間,以次類推。
#p#
三、Dialog Notification
一個對話框,用于遮擋當(dāng)前界面,使得當(dāng)前界面失去焦點(diǎn)。
通常用于鎖定屏幕,提示用戶等待等場景。例如:某個文件正在下載,出現(xiàn)提示等待。成功下載之后才能允許用戶進(jìn)行其他操作。
常用Dialog類型有:Alert Dialog、ProgressDialog、Custom Dialog
1.使用AlertDialog創(chuàng)建選擇窗口、列表窗口、單選窗口、多選窗口
1.1選擇窗口
效果:
創(chuàng)建彈窗方法:
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
 - builder.setCancelable(false);//設(shè)置當(dāng)點(diǎn)擊返回按鈕后,默認(rèn)表示的行為。這里設(shè)置為false
 - builder.setMessage("dialog彈窗標(biāo)題");
 - //設(shè)置true按鈕
 - builder.setPositiveButton("Yes", new OnClickListener() {
 - @Override
 - public void onClick(DialogInterface dialog, int which) {
 - Toast.makeText(getApplicationContext(), "您選擇了Yes", Toast.LENGTH_LONG).show();
 - }
 - });
 - //設(shè)置false按鈕
 - builder.setNegativeButton("No", new OnClickListener() {
 - @Override
 - public void onClick(DialogInterface dialog, int which) {
 - dialog.cancel();
 - }
 - });
 - //顯示
 - builder.show();
 
1.2列表窗口
效果:
創(chuàng)建方法:
- final String[] list = new String[]{"item1", "item2", "item3"};
 - AlertDialog.Builder builder = new AlertDialog.Builder(this);
 - builder.setTitle("dialog list彈窗標(biāo)題");
 - /**
 - * setItems(CharSequence[] items, OnClickListener listener)
 - * items:接收字符串?dāng)?shù)組,作為下拉列表選項
 - * listener:監(jiān)聽選中事件
 - */
 - builder.setItems(list, new OnClickListener() {
 - @Override
 - /**
 - * dialog:表示當(dāng)前彈窗對象
 - * which:表示當(dāng)前選中項的對應(yīng)list數(shù)組的序號
 - */
 - public void onClick(DialogInterface dialog, int which) {
 - Toast.makeText(getApplicationContext(), "您選擇了:"+list[which], Toast.LENGTH_LONG).show();
 - }
 - );
 - builder.show();
 
1.3單選列表彈窗
效果:
創(chuàng)建方法:
- final String[] list = new String[]{"item1", "item2", "item3"};
 - AlertDialog.Builder builder = new AlertDialog.Builder(this);
 - builder.setTitle("dialog list_single彈窗標(biāo)題");
 - /**
 - * setSingleChoiceItems(CharSequence[] items, int checkedItem, OnClickListener listener)
 - * items:下拉列表字符串?dāng)?shù)組
 - * checkedItem:默認(rèn)選中的數(shù)組序號,-1表示沒有默認(rèn)選中項
 - * listener:監(jiān)聽選中事件,注意!,單選、多選彈窗,當(dāng)選擇某個項時,默認(rèn)是不會關(guān)閉彈窗的。需要手動關(guān)閉。
 - */
 - builder.setSingleChoiceItems(list, -1, new OnClickListener() {
 - @Override
 - public void onClick(DialogInterface dialog, int which) {
 - Toast.makeText(getApplicationContext(), list[which], Toast.LENGTH_SHORT).show();
 - dialog.cancel();
 - //這里,當(dāng)用戶選中某個項時,提示選中文字,并關(guān)閉彈窗
 - }
 - });
 - builder.show();
 
1.4多選列表彈窗
效果:
創(chuàng)建方法:
- final String[] list = new String[]{"item1", "item2", "item3"};
 - AlertDialog.Builder builder = new AlertDialog.Builder(this);
 - builder.setTitle("dialog list_mutil彈窗標(biāo)題");
 - /**
 - * setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, OnMultiChoiceClickListener listener)
 - * items:下拉列表字符串?dāng)?shù)組
 - * checkedItems:boolean數(shù)組,如果需要默認(rèn)被選中,可以傳遞。null表示沒有默認(rèn)選中項
 - * listener:監(jiān)聽選中事件,注意!,單選、多選彈窗,當(dāng)選擇某個項時,默認(rèn)是不會關(guān)閉彈窗的。需要手動關(guān)閉。
 - */
 - builder.setMultiChoiceItems(list, null, new OnMultiChoiceClickListener() {
 - @Override
 - public void onClick(DialogInterface dialog, int which, boolean isChecked) {
 - Toast.makeText(getApplicationContext(), list[which], Toast.LENGTH_SHORT).show();
 - dialog.cancel();
 - }
 - });
 - builder.show();
 
2、自定義彈窗
如果需要自定義彈窗外觀,那么可以使用自定義彈窗。
下面一個自定義彈窗效果,并看看是如何實(shí)現(xiàn)的。
- AlertDialog.Builder builder;
 - LayoutInflater inflater = getLayoutInflater();
 - View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.layout_root));
 - TextView text = (TextView) layout.findViewById(R.id.text);
 - text.setText("這是自定義彈窗");
 - builder = new AlertDialog.Builder(getApplicationContext());
 - builder.setView(layout);
 - builder.show();
 
實(shí)際上自定義彈窗,就是使用自定義界面并覆蓋原有視圖內(nèi)容。






















 
 
 




 
 
 
 