Android中關(guān)于Notification及NotificationManger的詳解
Android狀態(tài)欄提醒
在Android中提醒功能也可以用AlertDialog,但是我們要慎重的使用,因為當(dāng)使用AlertDialog的時候,用戶正在進行的操作將會被打斷,因為當(dāng)前焦點被AlertDialog得到。我們可以想像一下,當(dāng)用戶打游戲正爽的時候,這時候來了一條短信。如果這時候短信用AlertDialog提醒,用戶必須先去處理這條提醒,從而才能繼續(xù)游戲。用戶可能會活活被氣死。而使用Notification就不會帶來這些麻煩事,用戶完全可以打完游戲再去看這條短信。所以在開發(fā)中應(yīng)根據(jù)實際需求,選擇合適的控件。
步驟:
一、添加布局對象
<Button
android:id="@+id/showButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="showNotification" />
<Button
android:id="@+id/cancelButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="cancelNotification" />
二、修改MianActivity繼承處Activity并實現(xiàn)接口OnClickListener
public class MainActivity extends Activity implements OnClickListener {
private Context mContext = this;
private Button showbtn, calclebtn;
private Notification noti;
private NotificationManager notiManger;
private static int NOTIFICATION_ID = 0x0001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpViews();
}
private void setUpViews() {
showbtn = (Button) findViewById(R.id.showButton);
calclebtn = (Button) findViewById(R.id.cancelButton);
noti = new Notification(R.drawable.ic_launcher, "this is a notification", System.currentTimeMillis());
noti.defaults = Notification.DEFAULT_SOUND;// 使用默認的提示聲音
noti.defaults |= Notification.DEFAULT_VIBRATE;// 添加震動
notiManger = (NotificationManager) this.getSystemService(mContext.NOTIFICATION_SERVICE);//獲取NofificationManger對象
showbtn.setOnClickListener(this);//讓Activity實現(xiàn)接口OnClickListener可以簡單的通過此兩行代碼添加按鈕點擊響應(yīng)事件
calclebtn.setOnClickListener(this);
}
// 按鈕點擊事件響應(yīng)
@Override
public void onClick(View v) {
if (v == showbtn) {
Intent intent = new Intent(this.getApplicationContext(),this.getClass());
// 設(shè)置Intent.FLAG_ACTIVITY_NEW_TASK
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
// noti.setLatestEventInfo(context, contentTitle, contentText, contentIntent)設(shè)置(上下文,標題,內(nèi)容,PendingInteng)
noti.setLatestEventInfo(this, "10086", "你從此以后免除所有話費", contentIntent);
// 發(fā)送通知(消息ID,通知對象)
notiManger.notify(NOTIFICATION_ID, noti);
} else if (v == calclebtn) {
// 取消通知(id)
notiManger.cancel(NOTIFICATION_ID);
}
}
}
相關(guān)文章
Android使用SoundPool實現(xiàn)播放音頻
這篇文章主要為大家詳細介紹了Android使用SoundPool實現(xiàn)播放音頻,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
Android Studio 3.1.3升級至3.6.1后舊項目的兼容操作方法
這篇文章主要介紹了Android Studio 3.1.3升級至3.6.1后舊項目的兼容操作方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Android開發(fā)ThreadPoolExecutor與自定義線程池詳解
這篇文章主要為大家介紹了Android開發(fā)ThreadPoolExecutor與自定義線程池詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android App開發(fā)中自定義View和ViewGroup的實例教程
這篇文章主要介紹了Android App開發(fā)中自定義View和ViewGroup的實例教程,分別介紹了進度條和圖片上傳并排列的例子,效果很好很強大,需要的朋友可以參考下2016-05-05
Android App數(shù)據(jù)格式Json解析方法和常見問題
JSON數(shù)據(jù)格式,在Android中被廣泛運用于客戶端和網(wǎng)絡(luò)(或者說服務(wù)器)通信,非常有必要系統(tǒng)的了解學(xué)習(xí)。恰逢本人最近對json做了一個簡單的學(xué)習(xí),特此總結(jié)一下,以饗各位2014-03-03
Android控件系列之Button以及Android監(jiān)聽器使用介紹
Button是各種UI中最常用的控件之一,它同樣也是Android開發(fā)中最受歡迎的控件之一,用戶可以通過觸摸它來觸發(fā)一系列事件,要知道一個沒有點擊事件的Button是沒有任何意義的,因為使用者的固定思維是見到它就想去點2012-11-11
android ListView和ProgressBar(進度條控件)的使用方法
這篇文章主要介紹了android ListView控件的使用方法和ProgressBar(進度條控件)的使用方法,代碼大家可以參考使用2013-11-11

