Android種使用Notification實(shí)現(xiàn)通知管理以及自定義通知欄實(shí)例(示例四)
示例一:實(shí)現(xiàn)通知欄管理
當(dāng)針對相同類型的事件多次發(fā)出通知,作為開發(fā)者,應(yīng)該避免使用全新的通知,這時(shí)就應(yīng)該考慮更新之前通知欄的一些值來達(dá)到提醒用戶的目的。例如我們手機(jī)的短信系統(tǒng),當(dāng)不斷有新消息傳來時(shí),我們的通知欄僅僅是更改傳來短信的數(shù)目,而不是對每條短信單獨(dú)做一個(gè)通知欄用于提示。
修改通知
可以設(shè)置一條通知,當(dāng)然可以更新一條通知,我們通過在調(diào)用NotificationManager.notify(ID, notification)時(shí)所使用的ID來更新它。為了更新你之前發(fā)布的通知,你需要更新或者創(chuàng)建一個(gè)NotificationCompat.Builder對象,從之前的通知中創(chuàng)建一個(gè)Notification對象,然后發(fā)布它使用你之前使用的ID。如果之前的通知仍然可見,系統(tǒng)將直接更新通知的內(nèi)容,如果之前的通知不可見了,一條新的通知將被創(chuàng)建。
下面的代碼演示了更新,以反映已發(fā)生的事件數(shù)量的通知。 它疊加通知,顯示的摘要:
(注意演示,通知數(shù)量會(huì)累積而且點(diǎn)擊通知后通知欄消失)
這里我們不再演示點(diǎn)擊按鈕以及跳轉(zhuǎn)頁面的布局文件,直接上java實(shí)現(xiàn)代碼:
import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.app.NotificationCompat; import android.view.View; import android.widget.RemoteViews; public class MainActivity extends AppCompatActivity { private static final int NO_1 = 0x1; int num =1;//初始通知數(shù)量為1 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //按鈕點(diǎn)擊事件(通知欄) public void show1(View v){ NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentTitle("新消息"); builder.setContentText("你有一條新的消息"); builder.setNumber(num++); //設(shè)置點(diǎn)擊通知跳轉(zhuǎn)頁面后,通知消失 builder.setAutoCancel(true); Intent intent = new Intent(this,Main2Activity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pi); Notification notification = builder.build(); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NO_1,notification); } }
當(dāng)我們設(shè)置setAutoCancel()為false時(shí),顯示效果如下(點(diǎn)擊后通知欄不消失):
示例二:自定義通知欄
通知的框架允許你去自定義通知的布局。通過RemoteViews對象來定義通知的外觀。自定義通知布局與常規(guī)通知相似,但是它是基于定義在XML文件的RemoteViews對象來操作的。
自定義通知的可用高度是取決于通知視圖的。正常的視圖布局高度限制在64dp,可展開視圖的布局高度限制在256dp。
為了去定義自己的通知布局,從擴(kuò)充XML文件獲取一個(gè)RemoteViews對象的實(shí)例開始。然后,類似于調(diào)用setContentTitle()方法一樣,我們需要調(diào)用setContent()。為了能設(shè)置更多細(xì)節(jié),我們使用RemoteViews對象的方法來設(shè)置更多的內(nèi)容。
1.創(chuàng)建一個(gè)單獨(dú)的XML文件,用來定義通知的布局。你可以使用任何你想用的名字,但后綴必須是.xml。
2.在應(yīng)用里面,使用RemoteViews對象的方法來給你的通知設(shè)置文本和圖標(biāo),通過調(diào)用setContent()把你的RemoteViews對象放到NotificationCompat.Builder里面。避免使用背景圖像,因?yàn)槟愕奈谋究赡軙?huì)變得不太好閱讀。
RemoteViews對象也包含一些方法用來給你去添加Chronometer和ProgressBar。想要了解更多自定義通知條布局的事情,參考RemoteViews的文檔。
注意:當(dāng)你使用自定義的通知條的時(shí)候,特別要注意你自定義的通知條在不同方向與分辨率的設(shè)備上是如何工作的。當(dāng)然這條建議對所有的視圖布局都很重要。但對通知條來說是尤其重要的,因?yàn)橥ㄖ閷系目丶值挠邢蕖2灰炎约旱耐ㄖ獥l做的太復(fù)雜,確保它的靈活性。
為自定義的通知條文本使用樣式資源(Usingstyle resources for custom notification text)
自定義通知條的時(shí)候總是使用樣式資源去定義文本。通知的背景顏色會(huì)變得與設(shè)備與當(dāng)前版本的android有很大的反差。使用樣式文件能幫你很好的解決這一點(diǎn)。從Android 2.3開始,系統(tǒng)就為標(biāo)準(zhǔn)的通知布局定義了文本的樣式,如果你在Android2.3 以及其更高的版本上使用同樣的樣式,你必須確保你的文本相對于背景是可以看見的。
注意:
Notification的自定義布局是RemoteViews,和其他RemoteViews一樣,在自定義視圖布局文件中,僅支持FrameLayout、LinearLayout、RelativeLayout三種布局控件和AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper這些顯示控件,不支持這些類的子類或Android提供的其他控件。否則會(huì)引起ClassNotFoundException異常。
帶按鈕的布局相應(yīng)點(diǎn)擊事件在3.0以下版本不啟作用。
下面簡單演示自定義音樂播放notification(未設(shè)置setongoing常駐):
Layout中message.xml(自定義布局):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="仗劍走天涯"/> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放"/> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一首"/> </LinearLayout>
MainActivity對應(yīng)java實(shí)現(xiàn)代碼的MainActivity.java(只演示點(diǎn)擊事件):
public void show2(View v){ NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.mipmap.guojia); RemoteViews rv = new RemoteViews(getPackageName(),R.layout.message); rv.setTextViewText(R.id.tv,"泡沫");//修改自定義View中的歌名 //修改自定義View中的圖片(兩種方法) // rv.setImageViewResource(R.id.iv,R.mipmap.ic_launcher); rv.setImageViewBitmap(R.id.iv, BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)); builder.setContent(rv); Notification notification = builder.build(); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NO_2,notification); }
至此notification相關(guān)知識(shí)點(diǎn)就總結(jié)完了,以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中通知Notification使用實(shí)例(振動(dòng)、燈光、聲音)
- Android中通過Notification&NotificationManager實(shí)現(xiàn)消息通知
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android 中Notification彈出通知實(shí)現(xiàn)代碼
- Android開發(fā) -- 狀態(tài)欄通知Notification、NotificationManager詳解
- Android 通知使用權(quán)(NotificationListenerService)的使用
- Android使用Notification在狀態(tài)欄上顯示通知
- android中創(chuàng)建通知欄Notification代碼實(shí)例
- Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能
- Android使用Notification實(shí)現(xiàn)通知功能
相關(guān)文章
Android文本視圖TextView實(shí)現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了Android文本視圖TextView實(shí)現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05kotlin 定義接口并實(shí)現(xiàn)回調(diào)的例子
這篇文章主要介紹了kotlin 定義接口并實(shí)現(xiàn)回調(diào)的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Jetpack Compose自定義動(dòng)畫與Animatable詳解
在今年的Google/IO大會(huì)上,亮相了一個(gè)全新的 Android 原生 UI 開發(fā)框架-Jetpack Compose, 與蘋果的SwiftIUI一樣,Jetpack Compose是一個(gè)聲明式的UI框架,這篇文章主要介紹了Jetpack Compose自定義動(dòng)畫與Animatable2022-10-10Android實(shí)現(xiàn)MVVM架構(gòu)數(shù)據(jù)刷新詳解流程
MVVM架構(gòu)模式,即Model-View-ViewModel三個(gè)層級(jí),MVVM模式出來的時(shí)間已經(jīng)很長了,網(wǎng)上關(guān)于MVVM模式的解析也有很多,我這里只說一下我自己的理解,基本上是和MVP模式相比較的一個(gè)差異2021-10-10Android?補(bǔ)間動(dòng)畫及組合AnimationSet常用方法詳解
這篇文章主要為大家介紹了Android?補(bǔ)間動(dòng)畫及組合AnimationSet常用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11點(diǎn)擊微信內(nèi)網(wǎng)頁a標(biāo)簽直接跳轉(zhuǎn)打開淘寶APP的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于如何實(shí)現(xiàn)點(diǎn)擊微信內(nèi)網(wǎng)頁a標(biāo)簽直接跳轉(zhuǎn)打開淘寶APP的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-11-11Android使用Intent隱式實(shí)現(xiàn)頁面跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Android使用Intent隱式來實(shí)現(xiàn)向上跳轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android自定義SeekBar實(shí)現(xiàn)滑動(dòng)驗(yàn)證且不可點(diǎn)擊
這篇文章主要為大家詳細(xì)介紹了Android自定義SeekBar實(shí)現(xiàn)滑動(dòng)驗(yàn)證且不可點(diǎn)擊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03