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