摘要:簡(jiǎn)介通知在用戶界面的一個(gè)重要部分,其使用方法請(qǐng)看以下內(nèi)容繼承關(guān)系如下簡(jiǎn)介通知是應(yīng)用向用戶顯示的消息提示,當(dāng)發(fā)送通知時(shí),通知將先以圖標(biāo)的形式顯示在通知區(qū)域中。通知區(qū)域和下拉通知欄均是由系統(tǒng)控制的區(qū)域,用戶可以隨時(shí)查看。
極力推薦文章:歡迎收藏
Android 干貨分享
本篇文章主要介紹 Android 開(kāi)發(fā)中的部分知識(shí)點(diǎn),通過(guò)閱讀本篇文章,您將收獲以下內(nèi)容:
Notification 簡(jiǎn)介
通知的創(chuàng)建
通知的管理
簡(jiǎn)單的通知
可以 擴(kuò)展的通知
通知中含下載進(jìn)度條
通知中含媒體播放控件
自定義通知內(nèi)容
Notification 通知是應(yīng)用向用戶顯示的消息提示,當(dāng)發(fā)送通知時(shí),通知將先以圖標(biāo)的形式顯示在通知區(qū)域中。用戶可以打開(kāi)下拉通知欄查看通知的詳細(xì)信息。 通知區(qū)域和下拉通知欄均是由系統(tǒng)控制的區(qū)域,用戶可以隨時(shí)查看。
Notification 簡(jiǎn)介通知在Android用戶界面的一個(gè)重要部分,其使用方法請(qǐng)看以下內(nèi)容:
Notification 繼承關(guān)系如下:java.lang.Object ???? android.app.Notification1.Notification 簡(jiǎn)介
通知是應(yīng)用向用戶顯示的消息提示,當(dāng)發(fā)送通知時(shí),通知將先以圖標(biāo)的形式顯示在通知區(qū)域中。用戶可以打開(kāi)下拉通知欄查看通知的詳細(xì)信息。 通知區(qū)域和下拉通知欄均是由系統(tǒng)控制的區(qū)域,用戶可以隨時(shí)查看。
2.創(chuàng)建Notification 的方法調(diào)用NotificationCompat.Builder.build() 創(chuàng)建Notification 對(duì)象
然后調(diào)用 NotificationManager.notify() 將 Notification 對(duì)象傳遞給系統(tǒng)。
Notification 對(duì)象必須包含以下內(nèi)容:小圖標(biāo),由 setSmallIcon() 設(shè)置
標(biāo)題,由 setContentTitle() 設(shè)置
詳細(xì)文本,由 setContentText() 設(shè)置
通知可選內(nèi)容設(shè)置優(yōu)先級(jí)
通知默認(rèn)優(yōu)先級(jí)為 PRIORITY_DEFAULT 0
Notification.Builder.setPriority()
5個(gè)級(jí)別可選(-2、-1、0、1、2)
通知優(yōu)先級(jí)如下:
PRIORITY_LOW=-1 PRIORITY_MIN=-2 PRIORITY_DEFAULT = 0 PRIORITY_HIGH=1 PRIORITY_MAX=2設(shè)置可以擴(kuò)展樣式
通過(guò)Notification.Builder.setStyle()可以設(shè)置通知的樣式。
點(diǎn)擊通知啟動(dòng)Activity(PendingIntent)通知中經(jīng)常遇到,點(diǎn)擊通知欄,打開(kāi) Activity。
Notification.Builder mBuilder = new Notification.Builder(this); mBuilder.setSmallIcon(R.drawable.gril) .setDefaults(Notification.DEFAULT_SOUND).setColor(000) .setContentTitle("簡(jiǎn)單通知Tittle").setContentText("點(diǎn)擊可以打開(kāi)Activity"); Intent resultIntent = new Intent(this, NotificationMethods.class); // 新開(kāi)一個(gè)Activity 棧 resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(NotificationMethods.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0, mBuilder.build());3. 通知的管理
更新通知
調(diào)用 NotificationManager.notify(ID) 發(fā)出帶有通知 ID 的通知,ID相同,即可更新以前ID發(fā)送的通知。
刪除通知
創(chuàng)建時(shí) 調(diào)用了 setAutoCancel(true)
刪除時(shí)候調(diào)用刪除指定ID
NotificationManager.cancel(notificationId)
刪除自己應(yīng)用發(fā)的所有通知
Utils.mNotificationManager.cancelAll();
在通知中顯示進(jìn)度條
setProgress()
4. 簡(jiǎn)單的通知實(shí)現(xiàn)效果
實(shí)現(xiàn)代碼
/** * 簡(jiǎn)單通知 */ public void SimpleNotification(View view) { Notification.Builder mBuilder = new Notification.Builder(this); mBuilder.setSmallIcon(R.drawable.gril) .setDefaults(Notification.DEFAULT_SOUND).setColor(000) .setContentTitle("簡(jiǎn)單通知Tittle").setContentText("點(diǎn)擊可以打開(kāi)Activity"); Intent resultIntent = new Intent(this, NotificationMethods.class); // 新開(kāi)一個(gè)Activity 棧 resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(NotificationMethods.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0, mBuilder.build()); }5. 可以 擴(kuò)展的通知
實(shí)現(xiàn)效果
實(shí)現(xiàn)代碼
/** * 可擴(kuò)展通知 * **/ public void NotificationStyle(View view) { Notification.Builder mBuilder = new Notification.Builder(this); mBuilder.setLargeIcon( DrawableUtils.DrawableToBitmap(getResources().getDrawable( R.drawable.ic_launcher))) .setContentTitle("我是可擴(kuò)展通知的Tittle ") .setDefaults(Notification.DEFAULT_SOUND) .setContentText("我是可擴(kuò)展通知的內(nèi)容") .setSmallIcon(R.drawable.ic_launcher) .setAutoCancel(true) .setStyle( new Notification.InboxStyle().addLine("我是可擴(kuò)展通知第一行") .addLine("我是可擴(kuò)展通知第二行") .setBigContentTitle("我是可擴(kuò)展的大 Tittle") .setSummaryText("點(diǎn)擊,展開(kāi)獲取更多內(nèi)容")); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 如果Id 一樣可以更新通知 mNotificationManager.notify(1, mBuilder.build()); }6. 通知中含下載進(jìn)度條
實(shí)現(xiàn)效果
實(shí)現(xiàn)代碼
/** * 帶有下載進(jìn)度條的通知 * **/ public void NotificationProcess(View view) { final NotificationManager mNotifyManagerProcess; final Notification.Builder mBuilder; mNotifyManagerProcess = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mBuilder = new Notification.Builder(this); mBuilder.setContentTitle("Picture Downloading").setSmallIcon( R.drawable.ic_launcher); new Thread(new Runnable() { @Override public void run() { for (MIncr = 0; MIncr <= 100; MIncr += 1 + 5 * Math.random()) { mBuilder.setProgress(100, MIncr, false).setContentText( MIncr + "%"); mNotifyManagerProcess.notify(2, mBuilder.build()); try { Thread.sleep(500); } catch (InterruptedException e) { } } /** * setProgress true 則表示 進(jìn)度條一直不停的從左至右滑動(dòng),類似于圓形進(jìn)度條 false :進(jìn)度條消失 * **/ mBuilder.setContentText("Download complete").setProgress(0, 0, false); mNotifyManagerProcess.notify(2, mBuilder.build()); } }).start(); }7. 通知中含媒體播放控件
實(shí)現(xiàn)效果
實(shí)現(xiàn)代碼
/** * 音樂(lè)播放器樣式 * **/ public void NotificationMediaStyle(View view) { Notification.Builder mMediaBuilder = new Notification.Builder(this); mMediaBuilder.setSmallIcon(R.drawable.ic_launcher); mMediaBuilder.setContentTitle("如果有一天我變有錢(qián)"); mMediaBuilder.setContentText("毛不易"); mMediaBuilder.setLargeIcon(DrawableUtils .DrawableToBitmap(getResources().getDrawable( R.drawable.ic_launcher))); Intent mIntent = new Intent(); ComponentName name = new ComponentName(this, NotificationMethods.class); mIntent.setComponent(name); PendingIntent mPendingIntent = PendingIntent.getActivity( getApplicationContext(), 0, mIntent, 0); mMediaBuilder.setContentIntent(mPendingIntent); mMediaBuilder.setPriority(Notification.PRIORITY_MAX); mMediaBuilder.addAction(new Notification.Action.Builder(Icon .createWithResource(NotificationMethods.this, R.drawable.music_pre), "1", null).build()); mMediaBuilder.addAction(new Notification.Action.Builder(Icon .createWithResource(NotificationMethods.this, R.drawable.music_play), "2", null).build()); mMediaBuilder.addAction(new Notification.Action.Builder(Icon .createWithResource(NotificationMethods.this, R.drawable.music_next), "3", null).build()); Notification.MediaStyle mMediaStyle = new Notification.MediaStyle(); mMediaStyle.setShowActionsInCompactView(0, 1, 2); mMediaBuilder.setStyle(mMediaStyle); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 如果Id 一樣可以更新通知 mNotificationManager.notify(1, mMediaBuilder.build()); }8. 自定義通知內(nèi)容
實(shí)現(xiàn)效果
實(shí)現(xiàn)代碼
/** * 自定義樣式通知 * **/ public void NotificationCustomView(View view) { /*** * 自定義Remoteview * **/ RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_view); remoteViews.setTextViewText(R.id.tv_content_title, "十年"); remoteViews.setTextViewText(R.id.tv_content_text, "陳奕迅"); // 打開(kāi)上一首 remoteViews.setOnClickPendingIntent(R.id.btn_pre, SetClickPendingIntent(NOTIFICATION_PRE)); // 打開(kāi)下一首 remoteViews.setOnClickPendingIntent(R.id.btn_next, SetClickPendingIntent(NOTIFICATION_NEXT)); // 點(diǎn)擊整體布局時(shí),打開(kāi)播放器 remoteViews.setOnClickPendingIntent(R.id.btn_play, SetClickPendingIntent(NOTIFICATION_PLAY)); // 點(diǎn)擊整體布局時(shí),打開(kāi)Activity remoteViews.setOnClickPendingIntent(R.id.ll_root, SetClickPendingIntent(NOTIFICATION_ACTIVITY)); remoteViews.setOnClickPendingIntent(R.id.img_clear, SetClickPendingIntent(NOTIFICATION_CANCEL)); Notification.Builder builder = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setTicker("當(dāng)前正在播放..") .setWhen(System.currentTimeMillis()) .setContentTitle("十年") .setContentText("陳奕迅") .setAutoCancel(true) .setLargeIcon( DrawableUtils.DrawableToBitmap(getResources() .getDrawable(R.drawable.ic_launcher))) .setContent(remoteViews); Utils.mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 打開(kāi)通知 Utils.mNotificationManager.notify(Utils.NOTIFICATION_CUSTOM_ID, builder.build()); } public PendingIntent SetClickPendingIntent(int what) { switch (what) { case NOTIFICATION_PRE: Intent intentPre = new Intent(this, MainActivity.class); intentPre.putExtra("cmd", what); int flagPre = PendingIntent.FLAG_UPDATE_CURRENT; PendingIntent clickPreIntent = PendingIntent.getActivity(this, what, intentPre, flagPre); return clickPreIntent; case NOTIFICATION_PLAY: Intent intentPlay = new Intent(this, NotificationMethods.class); intentPlay.putExtra("cmd", what); int flagPlay = PendingIntent.FLAG_UPDATE_CURRENT; PendingIntent clickPlayIntent = PendingIntent.getActivity(this, what, intentPlay, flagPlay); return clickPlayIntent; case NOTIFICATION_NEXT: Intent intentNext = new Intent(this, ActivityMethods.class); intentNext.putExtra("cmd", what); int flagNext = PendingIntent.FLAG_UPDATE_CURRENT; PendingIntent clickNextIntent = PendingIntent.getActivity(this, what, intentNext, flagNext); return clickNextIntent; case NOTIFICATION_ACTIVITY: Intent intentActivity = new Intent(this, ServiceMethod.class); intentActivity.putExtra("cmd", what); int flag = PendingIntent.FLAG_UPDATE_CURRENT; PendingIntent clickIntent = PendingIntent.getActivity(this, what, intentActivity, flag); Toast.makeText(getApplicationContext(), "打開(kāi)Activity", 0).show(); return clickIntent; case NOTIFICATION_CANCEL: Intent intentCancel = new Intent("Notification_cancel"); intentCancel.putExtra("cancel_notification_id", Utils.NOTIFICATION_CUSTOM_ID); int flagCancel = PendingIntent.FLAG_CANCEL_CURRENT; PendingIntent clickCancelIntent = PendingIntent.getBroadcast(this, 0, intentCancel, flagCancel); return clickCancelIntent; default: break; } return null; }
自定View 布局如下:
實(shí)現(xiàn)自定義通知?jiǎng)h除按鈕事件實(shí)現(xiàn)
case NOTIFICATION_CANCEL: Intent intentCancel = new Intent("Notification_cancel"); intentCancel.putExtra("cancel_notification_id", Utils.NOTIFICATION_CUSTOM_ID); int flagCancel = PendingIntent.FLAG_CANCEL_CURRENT; PendingIntent clickCancelIntent = PendingIntent.getBroadcast(this, 0, intentCancel, flagCancel); return clickCancelIntent;
廣播是四大組件之一,需要在AndroidManfest.xml 中注冊(cè)
注冊(cè)方式如下:
至此,本篇已結(jié)束,如有不對(duì)的地方,歡迎您的建議與指正。同時(shí)期待您的關(guān)注,感謝您的閱讀,謝謝!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/75842.html
摘要:只能執(zhí)行單一操作,無(wú)法返回結(jié)果給調(diào)用方,常用于網(wǎng)絡(luò)下載上傳文件,播放音樂(lè)等。綁定模式此模式通過(guò)綁定組件等調(diào)用啟動(dòng)此服務(wù)隨綁定組件的消亡而解除綁定。 showImg(https://segmentfault.com/img/remote/1460000019975019?w=157&h=54); 極力推薦文章:歡迎收藏Android 干貨分享 showImg(https://segme...
摘要:靜態(tài)注冊(cè)廣播的方法動(dòng)態(tài)注冊(cè)廣播在中動(dòng)態(tài)注冊(cè)廣播,通常格式如下動(dòng)態(tài)注冊(cè)廣播動(dòng)態(tài)注冊(cè)監(jiān)聽(tīng)滅屏點(diǎn)亮屏幕的廣播在廣播中動(dòng)態(tài)注冊(cè)廣播請(qǐng)注意一定要使用,防止為空,引起空指針異常。綁定模式此模式通過(guò)綁定組件等調(diào)用啟動(dòng)此服務(wù)隨綁定組件的消亡而解除綁定。 showImg(https://segmentfault.com/img/remote/1460000019975019?w=157&h=54); 極...
摘要:四大組件都支持這個(gè)屬性。到目前為止,中總共有三種啟動(dòng)方式。返回值方法有一個(gè)的返回值,這個(gè)返回值標(biāo)識(shí)服務(wù)關(guān)閉后系統(tǒng)的后續(xù)操作。,啟動(dòng)后的服務(wù)被殺死,不能保證系統(tǒng)一定會(huì)重新創(chuàng)建。 1. 簡(jiǎn)介 這篇文章會(huì)從Service的一些小知識(shí)點(diǎn),延伸到Android中幾種常用進(jìn)程間通信方法。 2. 進(jìn)程 ? ? ? ?Service是一種不提供用戶交互頁(yè)面但是可以在后臺(tái)長(zhǎng)時(shí)間運(yùn)行的組件,可以通過(guò)在An...
閱讀 2163·2021-11-22 15:22
閱讀 1302·2021-11-11 16:54
閱讀 1833·2021-09-23 11:32
閱讀 3028·2021-09-22 10:02
閱讀 1786·2019-08-30 12:59
閱讀 1100·2019-08-29 16:27
閱讀 634·2019-08-29 13:21
閱讀 2473·2019-08-28 17:57