Android Notification通知解析
Notification是顯示在手機(jī)狀態(tài)欄的通知,Notification通知是具有全局性的通知,一般通過NotificationManager來進(jìn)行管理.
一般運(yùn)用Notification的步驟如下:
- 1.調(diào)用getSysytemService(NOTIFICATION_SERVICE)來獲取系統(tǒng)的NotificationManager,進(jìn)行Notification的發(fā)送和回收
- 2.通過構(gòu)造器建立一個(gè)Notification
- 3.為Notification set各種屬性,然后builder()建立
- 4.通過NotificationManager發(fā)送通知
下面通過一個(gè)實(shí)例來演示上面的用法,先看一張效果圖
一.獲取系統(tǒng)的NotificationManager
private NotificationManager nm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取系統(tǒng)的通知管理 nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); }
二.為主布局的兩個(gè)按鈕添加監(jiān)聽事件,然后分別設(shè)置啟動(dòng)通知,并設(shè)置各種屬性和取消通知
各種屬性代碼中介紹的很詳細(xì),具體可以參考API
啟動(dòng)通知
public void send(View view){ //用于打開通知啟動(dòng)另一個(gè)Activity Intent intent = new Intent(MainActivity.this,OtherActivity.class); //用于延遲啟動(dòng) PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); //設(shè)置通知 Notification notify = new Notification.Builder(this) //設(shè)置打開該通知,通知自動(dòng)消失 .setAutoCancel(true) //設(shè)置顯示在狀態(tài)欄的通知提示消息 .setTicker("新消息") //設(shè)置通知欄圖標(biāo) .setSmallIcon(R.mipmap.ic_launcher) //設(shè)置通知內(nèi)容的標(biāo)題 .setContentTitle("一條新通知") //設(shè)置通知內(nèi)容 .setContentText("恭喜你通知欄測(cè)試成功") //設(shè)置使用系統(tǒng)默認(rèn)的聲音,默認(rèn)的led燈 .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS) //ALL的話則是全部使用默認(rèn),聲音,震動(dòng),閃光燈,需要添加相應(yīng)權(quán)限 // .setDefaults(ALL) //或者自定義聲音 //setSound(Uri.parse()) //設(shè)置要啟動(dòng)的程序 .setContentIntent(pi) //最后用build來建立通知 .build(); //發(fā)送當(dāng)前通知,通過NotificationManager來管理 nm.notify(1,notify); }
這里用的OtherActivity是通過通知啟動(dòng)的另一個(gè)Activity,為了啟動(dòng)需要在清單文件中加入此Activity,并且因?yàn)橛玫搅碎W光燈和振動(dòng)器,所以也需要添加相應(yīng)的權(quán)限
<activity android:name=".OtherActivity"> </activity> <uses-permission android:name="android.permission.FLASHLIGHT"/> <uses-permission android:name="android.permission.VIBRATE"/>
取消通知
//取消通知 public void closed(View view){ nm.cancel(1); }
用起來相當(dāng)很方便.最后附上主界面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:orientation="horizontal" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開啟通知" android:onClick="send" android:id="@+id/btnstartnotification" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="關(guān)閉通知" android:onClick="closed" android:id="@+id/btnstopnotification" /> </LinearLayout>
以上就是關(guān)于Android Notification通知的詳細(xì)內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android界面 NotificationManager使用Bitmap做圖標(biāo)
- Android中關(guān)于Notification及NotificationManger的詳解
- android notification 的總結(jié)分析
- android中創(chuàng)建通知欄Notification代碼實(shí)例
- Android中通過Notification&NotificationManager實(shí)現(xiàn)消息通知
- Android編程實(shí)現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法
- Android編程開發(fā)之NotiFication用法詳解
- 詳解Android中Notification通知提醒
相關(guān)文章
Android入門之BroadCast模擬實(shí)現(xiàn)異地登錄事件發(fā)生后的主動(dòng)退出
隨著對(duì)BroadCast的越來越深入,我們今天要實(shí)現(xiàn)一個(gè)稍微復(fù)雜一點(diǎn)的BroadCast。即只允許一個(gè)設(shè)備登錄一個(gè)帳號(hào)時(shí),APP會(huì)彈一個(gè)對(duì)話框如:您的賬號(hào)在別處登錄,請(qǐng)重新登陸!感興趣的可以了解一下2022-12-12Android編程簡(jiǎn)單實(shí)現(xiàn)雷達(dá)掃描效果
這篇文章主要介紹了Android編程簡(jiǎn)單實(shí)現(xiàn)雷達(dá)掃描效果,涉及Android圖形繪制及顯示的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10item高度不同時(shí)Recyclerview獲取滑動(dòng)距離的方法
這篇文章主要介紹了item高度不同時(shí)Recyclerview獲取滑動(dòng)距離的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11分享Android開發(fā)中最有效率最快的循環(huán)代碼
分享Android開發(fā)中最有效率最快的循環(huán)代碼,需要的朋友可以參考下2013-01-01Android WebView無法彈出軟鍵盤的原因及解決辦法
這篇文章主要介紹了Android WebView無法彈出軟鍵盤的原因及解決辦法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06解析Android中使用自定義字體的實(shí)現(xiàn)方法
本篇文章是對(duì)在Android中使用自定義字體的方法進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05Android實(shí)現(xiàn)顯示和隱藏密碼功能的示例代碼
在前端中我們知道用javascript就可以可以很容易實(shí)現(xiàn)密碼的顯示與隱藏,本文將大家詳細(xì)介紹Android是如何實(shí)現(xiàn)顯示和隱藏密碼功能的,需要的可以參考一下2022-06-06Android編程實(shí)現(xiàn)監(jiān)控各個(gè)程序流量的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)監(jiān)控各個(gè)程序流量的方法,涉及Android針對(duì)應(yīng)用包的遍歷,權(quán)限控制及相關(guān)屬性操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12