Android Notifications通知詳解
一、Toast Notifications
以背景改變方式,提示一些簡短的消息,消息窗口自動淡入淡出,不接受交互事件。
例如:當下載某個文件完成時,可以提示簡短的“保存成功”。
顯示效果:
創(chuàng)建彈出提示方法:
1、創(chuàng)建Toast對象,可以通過Toast提供的靜態(tài)方法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();
這樣,最簡單的提示信息已經(jīng)完成?,F(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文本組件用來顯示需要提示的文本。這里默認沒有設置文字。
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、創(chuàng)建Toast對象,并設置視圖、顯示視圖
- Toast toast = new Toast(getApplicationContext());
- //設置垂直居中,水平、垂直偏移值為0,表示正中間。
- toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);//設置提示框位置,三個參數(shù)分別代表:對其方式、水平偏移值、垂直偏移值。
- toast.setDuration(Toast.LENGTH_LONG);
- toast.setView(layout);//設置顯示的視圖
- toast.show();
顯示效果圖:
#p#
二、Status Bar Notification
狀態(tài)欄通知。當某個應用處于后臺運行時需要提示用戶某些信息時,不可能啟動Activity。這時使用狀態(tài)欄通知就非常合適。
例如:最經(jīng)典的就是當接收到新短信時,可以在通知欄看到簡要信息。
創(chuàng)建狀態(tài)欄通知的過程:
1.取得通知管理器
- NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
2.實例化通知對象
- /**
- * new Notification(int icon, String message, long when)
- * 參數(shù)1:通知圖標
- * 參數(shù)2:簡短提示文本
- * 參數(shù)3:何時顯示,這里使用的是時間戳
- */
- Notification notification = new Notification(R.drawable.icon, "狀態(tài)欄通知測試", 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);
這樣,就完成了一個簡單的狀態(tài)欄通知。
除此之外,還可以設置通知的提示方式,如震動、音樂、閃爍等。
設置提示聲音:
- notification.sound = Uri.parse("file:///sdcard/On Call.mp3");
設置震動的交替模式:
- notification.vibrate = new long[]{0,100,200,300};
這里vibrate是一個長整型數(shù)組,用來設置震動交替時長,第一個值表示震動開始之前,第二個值表示第一次震動的時間,第三個值表示第二次震動的時間,以次類推。
#p#
三、Dialog Notification
一個對話框,用于遮擋當前界面,使得當前界面失去焦點。
通常用于鎖定屏幕,提示用戶等待等場景。例如:某個文件正在下載,出現(xià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);//設置當點擊返回按鈕后,默認表示的行為。這里設置為false
- builder.setMessage("dialog彈窗標題");
- //設置true按鈕
- builder.setPositiveButton("Yes", new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(getApplicationContext(), "您選擇了Yes", Toast.LENGTH_LONG).show();
- }
- });
- //設置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彈窗標題");
- /**
- * setItems(CharSequence[] items, OnClickListener listener)
- * items:接收字符串數(shù)組,作為下拉列表選項
- * listener:監(jiān)聽選中事件
- */
- builder.setItems(list, new OnClickListener() {
- @Override
- /**
- * dialog:表示當前彈窗對象
- * which:表示當前選中項的對應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彈窗標題");
- /**
- * setSingleChoiceItems(CharSequence[] items, int checkedItem, OnClickListener listener)
- * items:下拉列表字符串數(shù)組
- * checkedItem:默認選中的數(shù)組序號,-1表示沒有默認選中項
- * listener:監(jiā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();
- //這里,當用戶選中某個項時,提示選中文字,并關閉彈窗
- }
- });
- 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彈窗標題");
- /**
- * setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, OnMultiChoiceClickListener listener)
- * items:下拉列表字符串數(shù)組
- * checkedItems:boolean數(shù)組,如果需要默認被選中,可以傳遞。null表示沒有默認選中項
- * listener:監(jiā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、自定義彈窗
如果需要自定義彈窗外觀,那么可以使用自定義彈窗。
下面一個自定義彈窗效果,并看看是如何實現(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();
實際上自定義彈窗,就是使用自定義界面并覆蓋原有視圖內容。