Android界面 NotificationManager使用Bitmap做圖標(biāo)
特寫(xiě)一個(gè)簡(jiǎn)單的demo供大家參考
今天發(fā)布的是NotificationManager 使用Bitmap做圖標(biāo)
關(guān)鍵code
public void notification(int flag)
{
Notification notification = new Notification();
//設(shè)置statusbar顯示的icon
notification.icon = R.drawable.icon;
//設(shè)置statusbar顯示的文字信息
// myNoti.tickerText= new_msg ;
notification.flags = Notification.FLAG_AUTO_CANCEL;
//設(shè)置notification發(fā)生時(shí)同時(shí)發(fā)出默認(rèn)聲音
notification.defaults = Notification.DEFAULT_SOUND;
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
Bitmap bitmap=null;
if(flag==0)
{
bitmap=drawableToBitmap(this.getResources().getDrawable(R.drawable.icon));
}else
{
//此處是關(guān)鍵地方,可以從網(wǎng)絡(luò)或是sdcard上獲取圖片,轉(zhuǎn)成bitmap就可以
bitmap=drawableToBitmap(this.getResources().getDrawable(R.drawable.alert_dialog_icon));
}
contentView.setImageViewBitmap(R.id.notification_icon, bitmap);
contentView.setTextViewText(R.id.app_name, "Custom notification");
notification.contentView = contentView;
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = contentIntent;
//顯示Notification
Random random = new Random(new Date().getTime());
mNotificationManager.notify(random.nextInt(1000000), notification);
}
//轉(zhuǎn)化drawableToBitmap
public static Bitmap drawableToBitmap(Drawable drawable)
{
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
源碼下載:NotificationIcon.rar
相關(guān)文章
Android實(shí)現(xiàn)沉浸式狀態(tài)欄
這篇文章主要為大家詳細(xì)介紹了Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android?PickerScrollView滑動(dòng)選擇控件使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android?PickerScrollView滑動(dòng)選擇控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android實(shí)現(xiàn)App中導(dǎo)航Tab欄懸浮的功能
相信大家在玩手機(jī)的過(guò)程中應(yīng)該會(huì)注意到很多的app都有這種功能,比如說(shuō)外賣(mài)達(dá)人常用的“餓了么”。所以這篇文章給大家分享了Android如何實(shí)現(xiàn)app中的導(dǎo)航Tab欄懸浮的功能,有需要的朋友們可以參考借鑒。2016-10-10
Handler消息傳遞機(jī)制類引入及執(zhí)行流程詳解
這篇文章主要為大家介紹了Handler消息傳遞機(jī)制類引入及執(zhí)行流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android?webview攔截H5的接口請(qǐng)求并返回處理好的數(shù)據(jù)代碼示例
這篇文章主要給大家介紹了關(guān)于Android?webview攔截H5的接口請(qǐng)求并返回處理好的數(shù)據(jù)的相關(guān)資料,通過(guò)WebView的shouldInterceptRequest方法,Android可以攔截并處理WebView中的H5網(wǎng)絡(luò)請(qǐng)求,需要的朋友可以參考下2024-10-10
PowerManagerService之自動(dòng)滅屏流程解析
這篇文章主要為大家介紹了PowerManagerService之自動(dòng)滅屏流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android Fragment動(dòng)態(tài)創(chuàng)建詳解及示例代碼
這篇文章主要介紹了Android Fragment動(dòng)態(tài)創(chuàng)建詳解的相關(guān)資料,并附實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11
Android編程雙重單選對(duì)話框布局實(shí)現(xiàn)與事件監(jiān)聽(tīng)方法示例
這篇文章主要介紹了Android編程雙重單選對(duì)話框布局實(shí)現(xiàn)與事件監(jiān)聽(tīng)方法,涉及Android雙重單選對(duì)話框的界面布局與事件監(jiān)聽(tīng)、響應(yīng)等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
詳解升級(jí)Android Studio3.0時(shí)遇到的幾個(gè)問(wèn)題
本篇文章主要介紹了升級(jí)Android Studio3.0時(shí)遇到的幾個(gè)問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10

