Android中使用Notification實(shí)現(xiàn)狀態(tài)欄的通知
在使用手機(jī)時(shí),當(dāng)有未接來電或者新短消息時(shí),手機(jī)會(huì)給出響應(yīng)的提示信息,這些提示信息通常會(huì)顯示到手機(jī)屏幕的狀態(tài)欄上。
Android也提供了用于處理這些信息的類,它們是Notification和NotificationManager。其中,Notification代表的是具有全局效果的通知,而NotificationManager則是用于發(fā)送Notification通知的系統(tǒng)服務(wù)。
使用Notification和NotificationManager類發(fā)送和顯示通知也比較簡(jiǎn)單,大致可以分為以下四個(gè)步驟
(1)調(diào)用getSystemService() 方法獲取系統(tǒng)的NotificationManager服務(wù)
(2)創(chuàng)建一個(gè)Notification對(duì)象,并為其設(shè)置各種屬性
(3)為Notification對(duì)象設(shè)置事件信息
(4)通過NotificationManager類的notify()方法發(fā)送Notification通知
下面通過一個(gè)實(shí)例說明和使用Notification在狀態(tài)欄上顯示通知
國際慣例
運(yùn)行結(jié)果:
布局文件就不發(fā)了 線性垂直布局 兩個(gè)按鈕
MainActivity.class
package com.example.notification; import android.os.Bundle; import android.app.Activity; import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{ private NotificationManager manager; private Button button1; private Button button2; private int Notification_ID; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); button1=(Button) findViewById(R.id.button1); button2=(Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.button1:{ showNotification(); break; } case R.id.button2:{ manager.cancel(Notification_ID); break; } } } private void showNotification() { // TODO Auto-generated method stub Notification.Builder builder=new Builder(this); builder.setSmallIcon(R.drawable.ic_launcher);//設(shè)置圖標(biāo) builder.setTicker("通知來啦");//手機(jī)狀態(tài)欄的提示 builder.setContentTitle("我是通知標(biāo)題");//設(shè)置標(biāo)題 builder.setContentText("我是通知內(nèi)容");//設(shè)置通知內(nèi)容 builder.setWhen(System.currentTimeMillis());//設(shè)置通知時(shí)間 Intent intent=new Intent(this,MainActivity.class); PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, 0); builder.setContentIntent(pendingIntent);//點(diǎn)擊后的意圖 builder.setDefaults(Notification.DEFAULT_LIGHTS);//設(shè)置指示燈 builder.setDefaults(Notification.DEFAULT_SOUND);//設(shè)置提示聲音 builder.setDefaults(Notification.DEFAULT_VIBRATE);//設(shè)置震動(dòng) Notification notification=builder.build();//4.1以上,以下要用getNotification() manager.notify(Notification_ID, notification); } }
上面代碼中設(shè)置的指示燈和震動(dòng),由于程序中要訪問系統(tǒng)的指示燈和振動(dòng)器 所以要在AndroidManifest.xml中聲明使用權(quán)限
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.FLASHLIGHT" />
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- Android使用Notification實(shí)現(xiàn)通知功能
- Android開發(fā)之Notification手機(jī)狀態(tài)欄通知用法實(shí)例分析
- Android使用Notification在狀態(tài)欄上顯示通知
- Android中Notification通知用法詳解
- Android 中Notification彈出通知實(shí)現(xiàn)代碼
- android 通知Notification詳解及實(shí)例代碼
- Android開發(fā)之Notification通知用法詳解
- Android中通知Notification的使用方法
- Android Notification通知使用詳解
相關(guān)文章
Android Studio打包H5網(wǎng)址頁面,封裝成APK
大家好,本篇文章主要講的是Android Studio打包H5網(wǎng)址頁面,封裝成APK,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android中定時(shí)執(zhí)行任務(wù)的3種實(shí)現(xiàn)方法(推薦)
下面小編就為大家?guī)硪黄狝ndroid中定時(shí)執(zhí)行任務(wù)的3種實(shí)現(xiàn)方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11Android開發(fā)實(shí)現(xiàn)的Log統(tǒng)一管理類
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的Log統(tǒng)一管理類,涉及Android日志管理及方法重載等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12Android自定義View葉子旋轉(zhuǎn)完整版(六)
這篇文章主要為大家詳細(xì)介紹了Android自定義View葉子旋轉(zhuǎn)完整版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android實(shí)現(xiàn)檢查并下載APK更新、安裝APK及獲取網(wǎng)絡(luò)信息的方法
這篇文章主要介紹了Android實(shí)現(xiàn)檢查并下載APK更新、安裝APK及獲取網(wǎng)絡(luò)信息的方法,很實(shí)用的功能,需要的朋友可以參考下2014-07-07Android實(shí)現(xiàn)價(jià)格走勢(shì)自定義曲線圖
本篇文章主要介紹了Android實(shí)現(xiàn)價(jià)格走勢(shì)曲線圖,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04