Android Notifications通知詳解
一、Toast Notifications
以背景改變方式,提示一些簡(jiǎn)短的消息,消息窗口自動(dòng)淡入淡出,不接受交互事件。
例如:當(dāng)下載某個(gè)文件完成時(shí),可以提示簡(jiǎn)短的“保存成功”。
顯示效果:
創(chuàng)建彈出提示方法:
1、創(chuàng)建Toast對(duì)象,可以通過(guò)Toast提供的靜態(tài)方法makeText(Context context, String message, int duration)
context:應(yīng)用上下文對(duì)象,這里可以傳遞getApplicationContext()
message:提示文本
duration:顯示時(shí)長(zhǎng),可以使用Toast.LENGTH_SHORT、Toast.LENGTH_LONG
- Context context = getApplicationContext();
- Toast toast = Toast.makeText(context, "保存成功", Toast.LENGTH_LONG);
2、顯示提示,調(diào)用show()方法
- toast.show();
上述兩步也可簡(jiǎn)寫(xiě)為:
- Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show();
這樣,最簡(jiǎn)單的提示信息已經(jīng)完成。現(xiàn)在來(lái)看看如何創(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文本組件用來(lái)顯示需要提示的文本。這里默認(rèn)沒(méi)有設(shè)置文字。
3.2、解析上述XML資源視圖,并設(shè)置提示文本
- LayoutInflater inflater = getLayoutInflater();//XML資源布局填充對(duì)象
- 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對(duì)象,并設(shè)置視圖、顯示視圖
- Toast toast = new Toast(getApplicationContext());
- //設(shè)置垂直居中,水平、垂直偏移值為0,表示正中間。
- toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);//設(shè)置提示框位置,三個(gè)參數(shù)分別代表:對(duì)其方式、水平偏移值、垂直偏移值。
- toast.setDuration(Toast.LENGTH_LONG);
- toast.setView(layout);//設(shè)置顯示的視圖
- toast.show();
顯示效果圖:
#p#
二、Status Bar Notification
狀態(tài)欄通知。當(dāng)某個(gè)應(yīng)用處于后臺(tái)運(yùn)行時(shí)需要提示用戶(hù)某些信息時(shí),不可能啟動(dòng)Activity。這時(shí)使用狀態(tài)欄通知就非常合適。
例如:最經(jīng)典的就是當(dāng)接收到新短信時(shí),可以在通知欄看到簡(jiǎn)要信息。
創(chuàng)建狀態(tài)欄通知的過(guò)程:
1.取得通知管理器
- NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
2.實(shí)例化通知對(duì)象
- /**
- * new Notification(int icon, String message, long when)
- * 參數(shù)1:通知圖標(biāo)
- * 參數(shù)2:簡(jiǎn)短提示文本
- * 參數(shù)3:何時(shí)顯示,這里使用的是時(shí)間戳
- */
- Notification notification = new Notification(R.drawable.icon, "狀態(tài)欄通知測(cè)試", System.currentTimeMillis());
3.定義通知的詳細(xì)信息、及PendIntent來(lái)設(shè)置激活的Activity
- //這里設(shè)置意圖處理很簡(jiǎn)單,僅僅是當(dāng)用戶(hù)觸摸詳細(xì)信息時(shí),將會(huì)顯示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.傳遞到通知管理器,加入到通知隊(duì)列
- manager.notify(11, notification);
這樣,就完成了一個(gè)簡(jiǎn)單的狀態(tài)欄通知。
除此之外,還可以設(shè)置通知的提示方式,如震動(dòng)、音樂(lè)、閃爍等。
設(shè)置提示聲音:
- notification.sound = Uri.parse("file:///sdcard/On Call.mp3");
設(shè)置震動(dòng)的交替模式:
- notification.vibrate = new long[]{0,100,200,300};
這里vibrate是一個(gè)長(zhǎng)整型數(shù)組,用來(lái)設(shè)置震動(dòng)交替時(shí)長(zhǎng),第一個(gè)值表示震動(dòng)開(kāi)始之前,第二個(gè)值表示第一次震動(dòng)的時(shí)間,第三個(gè)值表示第二次震動(dòng)的時(shí)間,以次類(lèi)推。
#p#
三、Dialog Notification
一個(gè)對(duì)話(huà)框,用于遮擋當(dāng)前界面,使得當(dāng)前界面失去焦點(diǎn)。
通常用于鎖定屏幕,提示用戶(hù)等待等場(chǎng)景。例如:某個(gè)文件正在下載,出現(xiàn)提示等待。成功下載之后才能允許用戶(hù)進(jìn)行其他操作。
常用Dialog類(lèi)型有: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ù)組,作為下拉列表選項(xiàng)
- * listener:監(jiān)聽(tīng)選中事件
- */
- builder.setItems(list, new OnClickListener() {
- @Override
- /**
- * dialog:表示當(dāng)前彈窗對(duì)象
- * which:表示當(dāng)前選中項(xiàng)的對(duì)應(yīng)list數(shù)組的序號(hào)
- */
- 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ù)組序號(hào),-1表示沒(méi)有默認(rèn)選中項(xiàng)
- * listener:監(jiān)聽(tīng)選中事件,注意!,單選、多選彈窗,當(dāng)選擇某個(gè)項(xiàng)時(shí),默認(rèn)是不會(huì)關(guān)閉彈窗的。需要手動(dòng)關(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)用戶(hù)選中某個(gè)項(xiàng)時(shí),提示選中文字,并關(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表示沒(méi)有默認(rèn)選中項(xiàng)
- * listener:監(jiān)聽(tīng)選中事件,注意!,單選、多選彈窗,當(dāng)選擇某個(gè)項(xiàng)時(shí),默認(rèn)是不會(huì)關(guān)閉彈窗的。需要手動(dòng)關(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、自定義彈窗
如果需要自定義彈窗外觀,那么可以使用自定義彈窗。
下面一個(gè)自定義彈窗效果,并看看是如何實(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)容。