<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                #### 5.1.1 RemoteViews在通知欄上的應用 首先我們看一下RemoteViews在通知欄上的應用,我們知道,通知欄除了默認的效果外還支持自定義布局,下面分別說明這兩種情況。 使用系統默認的樣式彈出一個通知是很簡單的,代碼如下: Notification notification = new Notification(); notification.icon = R.drawable.ic_launcher; notification.tickerText = "hello world"; notification.when = System.currentTimeMillis(); notification.flags = Notification.FLAG_AUTO_CANCEL; Intent intent = new Intent(this, DemoActivity_1.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(this, "chapter_5", "this is notification.", pendingIntent); NotificationManager manager = (NotificationManager)getSystemService (Context.NOTIFICATION_SERVICE); manager.notify(1, notification); 上述代碼會彈出一個系統默認樣式的通知,單擊通知后會打開DemoActivity_1同時會清除本身。為了滿足個性化需求,我們還可能會用到自定義通知。自定義通知也很簡單,首先我們要提供一個布局文件,然后通過RemoteViews來加載這個布局文件即可改變通知的樣式,代碼如下所示。 Notification notification = new Notification(); notification.icon = R.drawable.ic_launcher; notification.tickerText = "hello world"; notification.when = System.currentTimeMillis(); notification.flags = Notification.FLAG_AUTO_CANCEL; Intent intent = new Intent(this, DemoActivity_1.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout. layout_notification); remoteViews.setTextViewText(R.id.msg, "chapter_5"); remoteViews.setImageViewResource(R.id.icon, R.drawable.icon1); PendingIntent openActivity2PendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, DemoActivity_2.class), PendingIntent.FLAG_ UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.open_activity2, openActivity2- PendingIntent); notification.contentView = remoteViews; notification.contentIntent = pendingIntent; NotificationManager manager = (NotificationManager)getSystemService (Context.NOTIFICATION_SERVICE); manager.notify(2, notification); 從上述內容來看,自定義通知的效果需要用到RemoteViews,自定義通知的效果如圖5-1所示。 :-: ![](https://img.kancloud.cn/03/ae/03aea407cf1232d5cdf20ba03cb96704_535x609.png) 圖5-1 自定義通知欄樣式 RemoteViews的使用也很簡單,只要提供當前應用的包名和布局文件的資源id即可創建一個RemoteViews對象。如何更新RemoteViews呢?這一點和更新View有很大的不同,更新RemoteViews時,無法直接訪問里面的View,而必須通過RemoteViews所提供的一系列方法來更新View。比如設置TextView的文本,要采用如下方式:remoteViews. setTextViewText(R.id.msg, "chapter_5"),其中setTextViewText的兩個參數分別為TextView的id和要設置的文本。而設置ImageView的圖片也不能直接訪問ImageView,必須通過如下方式:remoteViews.setImageViewResource(R.id.icon, R.drawable.icon1), setImageViewResource的兩個參數分別為ImageView的id和要設置的圖片資源的id。如果要給一個控件加單擊事件,則要使用PendingIntent并通過setOnClickPendingIntent方法來實現,比如remoteViews.setOnClickPendingIntent(R.id.open_activity2, openActivity2Pending- Intent)這句代碼會給id為open_activity2的View加上單擊事件。關于PendingIntent,它表示的是一種待定的Intent,這個Intent中所包含的意圖必須由用戶來觸發。為什么更新RemoteViews如此復雜呢?直觀原因是因為RemoteViews并沒有提供和View類似的findViewById這個方法,因此我們無法獲取到RemoteViews中的子View,當然實際原因絕非如此,具體會在5.2節中進行詳細介紹。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看