安卓基礎知識之通知系統

Toast、Notification、Dialog

一、Toast

Toast.makeText(this, "提示消息一", Toast.LENGTH_SHORT).show();

第三個參數:顯示的時間 Toast.LENGTH_SHORT

Toast.LENGTH_LONG

特性:1、Toast提示不會獲取焦點

2、Toast提示消息過一段時間會自己消失。

應用場景:提示用戶當前狀態,不需要用戶確認、反饋。在其他頁面仍然可以看到結果。


二、Notification

Notification

顯示在手機狀態欄的通知。它代表的是一種全局效果的通知。


//1、得到一個消息管理器

manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


//消息發送的時機(自定)


//2、創建一個消息對象

Notification notification =

new Notification(R.drawable.ic_launcher, "通知1", System.currentTimeMillis());

//5、設置意圖對象

Intent intent = new Intent(this, SecondActivity.class);

//4、設置關聯的Activity

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent , 0);

//3、設置消息的主體內容

notification.setLatestEventInfo(this, "標題一", "內容一", contentIntent );

notification.flags = Notification.FLAG_AUTO_CANCEL;//點擊完自動消失

//notification.flags = Notification.FLAG_ONGOING_EVENT;//點擊之後不會消失

//6、通過消息管理器發送一條消息

manager.notify(123, notification );

//銷燬消息

manager.cancel(123);

Notification:

作用:來消息的時候,在通知欄上面顯示(時效性不強的消息),當用戶點擊這個消息的時候

跳轉到詳細頁面查看詳細消息。

常見:短信

廣告

注:在4.1之後新的方法:Notification.Builder(context)

三、Dialog

//1、首先得到一個builder對象

AlertDialog.Builder builder = new AlertDialog.Builder(this);

//2、通過builder對象設置對話框內容

builder.setTitle("Dialog");//z設置對話框標題

builder.setMessage("消息!");//設置對話框消息內容

//設置對話框按鈕,按鈕對象只與位置有關

builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

startActivity(new Intent(MainActivity.this, SecondActivity.class));

}

});

builder.setNegativeButton("取消", null);

builder.setNeutralButton("應用", null);

//3、通過builder對象創建一個AlertDialog對象

AlertDialog dialog = builder.create();

//4、把AlertDialog展示出去。

dialog.show();



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章