Android不使用自定義布局情況下實現(xiàn)自定義通知欄圖標的方法
本文實例講述了Android不使用自定義布局情況下實現(xiàn)自定義通知欄圖標的方法。分享給大家供大家參考,具體如下:
自定義通知欄圖標?不是很簡單么。自定義布局都不在話下!
是的,有xml布局文件當然一切都很簡單,如果不給你布局文件用呢?
聽我慢慢道來!
首先怎么創(chuàng)建一個通知呢?
1.new 一個
參數(shù):圖標 ID,發(fā)送到狀態(tài)欄瞬間的文字,當前時間
2.設(shè)置詳細信息:標題、內(nèi)容、intent
PendingIntent contentIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); n.setLatestEventInfo(this, "早上好!", "今天是個晴朗的天氣!", contentIntent);
3.發(fā)送到通知欄
NotificationManager mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNM.notify(1001, n);
這樣就完成了一個通知的展示,很簡單!
我們來看看 n.setLatestEventInfo 干了些什么呢
public void setLatestEventInfo(Context context,
CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
// TODO: rewrite this to use Builder
RemoteViews contentView = new RemoteViews(context.getPackageName(),
R.layout.notification_template_base);
if (this.icon != 0) {
contentView.setImageViewResource(R.id.icon, this.icon);
}
if (priority < PRIORITY_LOW) {
contentView.setInt(R.id.icon,
"setBackgroundResource", R.drawable.notification_template_icon_low_bg);
contentView.setInt(R.id.status_bar_latest_event_content,
"setBackgroundResource", R.drawable.notification_bg_low);
}
if (contentTitle != null) {
contentView.setTextViewText(R.id.title, contentTitle);
}
if (contentText != null) {
contentView.setTextViewText(R.id.text, contentText);
}
if (this.when != 0) {
contentView.setViewVisibility(R.id.time, View.VISIBLE);
contentView.setLong(R.id.time, "setTime", when);
}
if (this.number != 0) {
NumberFormat f = NumberFormat.getIntegerInstance();
contentView.setTextViewText(R.id.info, f.format(this.number));
}
this.contentView = contentView;
this.contentIntent = contentIntent;
}
可以看到,他實際上就是使用系統(tǒng)默認布局為我們創(chuàng)建了一個 RemoteViews ,RemoteViews 是專門用來跨進程顯示的 View ,詳情參考官方文檔:http://developer.android.com/intl/zh-cn/reference/android/widget/RemoteViews.html
看這句:
其實就是設(shè)置圖標了:
參數(shù)1:用來顯示圖標的 ImageView 的 id
參數(shù)2:圖標 id
但是還有一個這樣的方法:
用 Bitmap 來設(shè)置圖標。
而 Notifycation 里面有個參數(shù):Notification.contentView,仔細看,setLastestEventInfo 方法里創(chuàng)建的 RemoteViews 就是他,所以你知道該怎么做了!
但是這里還有一個問題?R.id.icon 怎么獲取,這個東西其實在 com.android.internal.R 這個里面,但是這個類我們訪問不到怎么辦?
反射唄, Java 的反射可謂是萬能啊,啥都可以拿到只要他在。
Class<?> clazz = Class.forName("com.android.internal.R$id");
Field field = clazz.getField("icon");
field.setAccessible(true);
int id_icon = field.getInt(null);
n.setLatestEventInfo(context, title, msg, contentIntent);
n.flags |= Notification.FLAG_AUTO_CANCEL;
if(n.contentView != null && icon != null){
n.contentView.setImageViewBitmap(id_icon, icon);
}
發(fā)出通知,下拉通知欄看看,圖標是不是變了^_^
此外這里還有一個小細節(jié),就是你 new Notifycation() 是傳進去的圖標會作為狀態(tài)欄的小圖標,小圖標尺寸在 hdpi 下面放 32x32 的就可以

所以你可以第一次傳小圖標,然后通過 contentView 設(shè)置大圖標,這樣就OK了

希望本文所述對大家Android程序設(shè)計有所幫助。
- Android實現(xiàn)沉浸式通知欄通知欄背景顏色跟隨app導(dǎo)航欄背景顏色而改變
- Android開發(fā)之禁止下拉通知欄的方法
- android實現(xiàn)通知欄下載更新app示例
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android開發(fā)之使用通知欄顯示提醒信息的方法
- 關(guān)于Android中點擊通知欄的通知啟動Activity問題解決
- Android程序版本更新之通知欄更新下載安裝
- android中創(chuàng)建通知欄Notification代碼實例
- Android實現(xiàn)通知欄透明的方法
- Android Fragment實現(xiàn)底部通知欄
相關(guān)文章
Android studio 下JNI編程實例并生成so庫的實現(xiàn)代碼
這篇文章主要介紹了Android studio 下JNI編程實例并生成so庫,需要的朋友可以參考下2017-09-09
Android實現(xiàn)發(fā)送短信驗證碼倒計時功能示例
本篇文章主要介紹了Android實現(xiàn)發(fā)送短信驗證碼倒計時功能示例,這里整理了詳細的代碼,有需要的小伙伴可以參考下。2017-03-03
Flutter List數(shù)組避免插入重復(fù)數(shù)據(jù)的實現(xiàn)
這篇文章主要介紹了Flutter List數(shù)組避免插入重復(fù)數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
基于Android在布局中動態(tài)添加view的兩種方法(總結(jié))
下面小編就為大家?guī)硪黄贏ndroid在布局中動態(tài)添加view的兩種方法(總結(jié))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10

