<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Notification 通用使用筆記

          共 15620字,需瀏覽 32分鐘

           ·

          2024-05-21 08:12

          和你一起終身學(xué)習(xí),這里是程序員Android

          經(jīng)典好文推薦,通過閱讀本文,您將收獲以下知識(shí)點(diǎn):

          一、Notification 簡(jiǎn)介
          二、創(chuàng)建Notification 的方法
          三、通知的管理
          四、簡(jiǎn)單的通知實(shí)現(xiàn)
          五、可擴(kuò)展通知實(shí)現(xiàn)
          六、通知中含下載進(jìn)度條
          七、通知中含媒體播放控件
          八、自定義通知內(nèi)容

          一、Notification 簡(jiǎn)介

          Notification 通知是應(yīng)用向用戶顯示的消息提示,當(dāng)發(fā)送通知時(shí),通知將先以圖標(biāo)的形式顯示在通知區(qū)域中。用戶可以打開下拉通知欄查看通知的詳細(xì)信息。通知區(qū)域和下拉通知欄均是由系統(tǒng)控制的區(qū)域,用戶可以隨時(shí)查看。

          通知在Android用戶界面的一個(gè)重要部分,其使用方法請(qǐng)看以下內(nèi)容:

          通知區(qū)域中的通知

          下拉通知欄中的通知

          二、創(chuàng)建Notification 的方法

          • 1.通知?jiǎng)?chuàng)建的方法

          調(diào)用NotificationCompat.Builder.build() 創(chuàng)建Notification對(duì)象,然后調(diào)用 NotificationManager.notify() 將Notification對(duì)象傳遞給系統(tǒng)。

          • 2.Notification 對(duì)象必須包含以下內(nèi)容:

            • 小圖標(biāo),由setSmallIcon()設(shè)置

            • 標(biāo)題,由 setContentTitle() 設(shè)置

            • 詳細(xì)文本,由 setContentText()設(shè)置

          • 3.通知可選內(nèi)容

          通知默認(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
          • 4.設(shè)置可以擴(kuò)展樣式

          通過Notification.Builder.setStyle()可以設(shè)置通知的樣式。

          • 5.點(diǎn)擊通知啟動(dòng)Activity(PendingIntent)

          通知中經(jīng)常遇到,點(diǎn)擊通知欄,打開 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)擊可以打開Activity");

          Intent resultIntent = new Intent(this, NotificationMethods.class);
          // 新開一個(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());

          三、通知的管理

          • 1.更新通知

          調(diào)用 NotificationManager.notify(ID) 發(fā)出帶有通知ID的通知,ID相同,即可更新以前ID發(fā)送的通知。

          • 2.刪除通知

          創(chuàng)建時(shí) 調(diào)用了 setAutoCancel(true)
          刪除時(shí)候調(diào)用刪除指定ID

          NotificationManager.cancel(notificationId)

          刪除自己應(yīng)用發(fā)的所有通知

          Utils.mNotificationManager.cancelAll();
          • 3.在通知中顯示進(jìn)度條

          setProgress()

          四、簡(jiǎn)單的通知實(shí)現(xiàn)

          • 1.實(shí)現(xiàn)效果

          簡(jiǎn)單通知圖片

          • 2.實(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)擊可以打開Activity");

          Intent resultIntent = new Intent(this, NotificationMethods.class);
          // 新開一個(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());
          }

          五、可擴(kuò)展通知實(shí)現(xiàn)

          • 1.實(shí)現(xiàn)效果

          通知展開圖

          通知未展開圖

          • 2.實(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)擊,展開獲取更多內(nèi)容"));

          NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          // 如果Id 一樣可以更新通知
          mNotificationManager.notify(1, mBuilder.build());
          }

          六、通知中含下載進(jìn)度條

          • 1.實(shí)現(xiàn)效果

          下載中通知

          下載完成通知

          • 2.實(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();
          }

          七、通知中含媒體播放控件

          • 1.實(shí)現(xiàn)效果

          未展開圖

          展開圖

          • 2.實(shí)現(xiàn)代碼

              /**
          * 音樂播放器樣式
          * **/

          public void NotificationMediaStyle(View view) {
          Notification.Builder mMediaBuilder = new Notification.Builder(this);
          mMediaBuilder.setSmallIcon(R.drawable.ic_launcher);
          mMediaBuilder.setContentTitle("如果有一天我變有錢");
          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());
          }

          八、自定義通知內(nèi)容

          • 1.實(shí)現(xiàn)效果

          自定義通知效果圖

          • 2.實(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, "陳奕迅");
          // 打開上一首
          remoteViews.setOnClickPendingIntent(R.id.btn_pre,
          SetClickPendingIntent(NOTIFICATION_PRE));
          // 打開下一首
          remoteViews.setOnClickPendingIntent(R.id.btn_next,
          SetClickPendingIntent(NOTIFICATION_NEXT));
          // 點(diǎn)擊整體布局時(shí),打開播放器
          remoteViews.setOnClickPendingIntent(R.id.btn_play,
          SetClickPendingIntent(NOTIFICATION_PLAY));
          // 點(diǎn)擊整體布局時(shí),打開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);
          // 打開通知
          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(), "打開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;

          }
          • 3.自定View布局如下:

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/ll_root"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="#282828"
          android:gravity="center_vertical"
          android:orientation="horizontal"
          android:padding="8dp" >


          <ImageView
          android:layout_width="50dp"
          android:layout_height="50dp"
          android:padding="5dp"
          android:src="@drawable/ic_launcher" />


          <LinearLayout
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:gravity="center_vertical"
          android:orientation="vertical" >


          <TextView
          android:id="@+id/tv_content_title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="5dp"
          android:textSize="18sp"
          android:text="十年"
          android:textColor="@android:color/white" />


          <TextView
          android:id="@+id/tv_content_text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:paddingLeft="5dp"
          android:text="陳奕迅"
          android:textSize="14sp"
          android:textColor="@android:color/white" />

          </LinearLayout>

          <Button
          android:id="@+id/btn_pre"
          android:layout_width="30dp"
          android:layout_height="30dp"
          android:background="@drawable/music_pre" />


          <Button
          android:id="@+id/btn_play"
          android:layout_width="30dp"
          android:layout_height="30dp"
          android:layout_marginLeft="16dp"
          android:layout_marginRight="8dp"
          android:background="@drawable/music_play" />


          <Button
          android:id="@+id/btn_next"
          android:layout_width="30dp"
          android:layout_height="30dp"
          android:layout_marginLeft="16dp"
          android:layout_marginRight="8dp"
          android:background="@drawable/music_next" />


          <ImageView
          android:id="@+id/img_clear"
          android:layout_width="30dp"
          android:layout_height="match_parent"
          android:layout_gravity="top|center_horizontal"
          android:padding="6dp"
          android:src="@drawable/clear_img" />


          </LinearLayout>
          • 4.實(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;
          • 5.廣播是四大組件之一,需要在AndroidManfest.xml中注冊(cè)

          注冊(cè)方式如下:

                  <receiver
          android:name="com.programandroid.BroadcastReceiver.NotificationReceived"
          android:enabled="true"
          android:exported="true" >

          <intent-filter>
          <action android:name="Notification_cancel" />
          <action android:name="Notification_music_pre" />
          <action android:name="Notification_music_play" />
          <action android:name="Notification_music_next" />
          </intent-filter>
          </receiver>

          至此,本篇已結(jié)束。轉(zhuǎn)載網(wǎng)絡(luò)的文章,小編覺得很優(yōu)秀,歡迎點(diǎn)擊閱讀原文,支持原創(chuàng)作者,如有侵權(quán),懇請(qǐng)聯(lián)系小編刪除。同時(shí)感謝您的閱讀,期待您的關(guān)注。

          點(diǎn)個(gè)在看,方便您使用時(shí)快速查找!

          瀏覽 60
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产免费一区二区三区四区午夜视频 | 免费国产黄色 | 亚洲无码中文字幕在线观看视频 | 最近中文字幕免费mv第一季歌词在线观看 | 肏屄视频在线免费观看 |