溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

安卓基礎(chǔ)知識之通知系統(tǒng)

發(fā)布時間:2020-07-17 14:52:05 來源:網(wǎng)絡(luò) 閱讀:759 作者:android09 欄目:開發(fā)技術(shù)

Toast、Notification、Dialog

一、Toast

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

第三個參數(shù):顯示的時間 Toast.LENGTH_SHORT

Toast.LENGTH_LONG

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

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

應(yīng)用場景:提示用戶當(dāng)前狀態(tài),不需要用戶確認(rèn)、反饋。在其他頁面仍然可以看到結(jié)果。


二、Notification

Notification

顯示在手機(jī)狀態(tài)欄的通知。它代表的是一種全局效果的通知。


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

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


//消息發(fā)送的時機(jī)(自定)


//2、創(chuàng)建一個消息對象

Notification notification =

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

//5、設(shè)置意圖對象

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

//4、設(shè)置關(guān)聯(lián)的Activity

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

//3、設(shè)置消息的主體內(nèi)容

notification.setLatestEventInfo(this, "標(biāo)題一", "內(nèi)容一", contentIntent );

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

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

//6、通過消息管理器發(fā)送一條消息

manager.notify(123, notification );

//銷毀消息

manager.cancel(123);

Notification:

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

跳轉(zhuǎn)到詳細(xì)頁面查看詳細(xì)消息。

常見:短信

廣告

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

三、Dialog

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

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

//2、通過builder對象設(shè)置對話框內(nèi)容

builder.setTitle("Dialog");//z設(shè)置對話框標(biāo)題

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

//設(shè)置對話框按鈕,按鈕對象只與位置有關(guān)

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("應(yīng)用", null);

//3、通過builder對象創(chuàng)建一個AlertDialog對象

AlertDialog dialog = builder.create();

//4、把AlertDialog展示出去。

dialog.show();



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI