Android Notification詳解——響應notification事件

來源:互聯網
上載者:User

標籤:

上一篇講了如何建立並顯示一個notification,這一篇就總結下點擊notification後,程式應該如何響應。

 

一般來講,點擊一個notification後,都會開啟一個Activity做為對點擊事件的響應,這個Activity是之前在PendingIntent中設定好的。

經常玩Android手機的應該都有印象,在日曆應用中,你建立一個提醒,當提醒通知收到後,你點擊通知,會進入提醒的內容頁面,如果這個時候按back鍵,會直接退出應用。

但是在Gmail的應用中,如果有一封新郵件到來,那麼點擊通知後,會進入到郵件的內容頁面,等你看完郵件,點擊back鍵,會退到郵件清單頁面,再按back鍵,才會退出應用。

 

我們總結一下兩種情況,假設我們的應用有兩個Activity(ParentActivity、SubActivity),notification中設定開啟的Activity為SubActivity。

那麼第一種情況就是:

點擊Notification ——>進入SubActivity ——> back鍵 ——> 退出應用

第二種情況:

點擊Notification ——>進入SubActivity ——> back鍵 ——> 退到ParentActivity ——>back鍵 ——>退出應用

 

第一種情況比較簡單,只需要在PendingIntent中指定Activity,不需要其他設定,Android預設的就這樣。

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,  intent, PendingIntent.FLAG_CANCEL_CURRENT);

但是在建立PendingIntent的時候需要注意參數PendingIntent.FLAG_CANCEL_CURRENT

這個標誌位用來指示:如果當前的Activity和PendingIntent中設定的intent一樣,那麼久先取消當前的Activity,用PendingIntent中指定的Activity取代之。

另外,需要在Manifest中對指定的Activity設定屬性

<activity android:name=".SubActivityl"          android:launchMode="singleTask"          android:taskAffinity=""          android:excludeFromRecents="true">  </activity>

第二種情況稍微複雜點,因為如果只開啟一個SubActivity,程式並沒辦法知道他的上一級Activity是誰,所以需要在點擊Notification時開啟一組Activity,但是我們並不需要一個個去調用startActivity方法,PendingIntent提供了個靜態方法getActivities,裡面可以設定一個Intent數組,用來指定一系列的Activity。

所以我們首先寫一個函數建立一個Activity數組:

Intent[] makeIntentStack(Context context) {      Intent[] intents = new Intent[2];      intents[0] = Intent.makeRestartActivityTask(new ComponentName(context, com.example.notificationtest.MainActivity.class));      intents[1] = new Intent(context,  com.example.notificationtest.SubActivity.class);      return intents;  }

其中需要注意的是Intent.makeRestartActivityTask方法,這個方法用來建立activity棧的根activity

接下來,建立並顯示Notification:

void showNotification(Intent intent) {      Notification notification = new Notification(              R.drawable.status_icon,               "Hello World ticker text",              System.currentTimeMillis());        PendingIntent contentIntent = PendingIntent.getActivities(              this,              0,              makeIntentStack(this),               PendingIntent.FLAG_CANCEL_CURRENT);      notification.setLatestEventInfo(              this,               "Title",              "Hey, shall we have a dinner tonight",               contentIntent);      notification.flags |= Notification.DEFAULT_ALL;        mNM.notify(1, notification);  }


Android Notification詳解——響應notification事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.