想必不少朋友,寫的android的程序在後臺service(用service的好處在於它可以獨立於app運行,甚至可以通過使用bootloader設置開機啓動,使用log可以看到app中的service和app是不同的pid,由此證明他們兩個處於不同的進程滯後)產生通知消息。
當用戶點擊通知消息的時候需要調出activity顯示界面。
可是這個時候,界面activity有五種情況:
1、未開啓
2、此activity之前的activity開啓(順序是按照正常點擊應用的時候,讓此activity出現的次序,之前表示調用此activity的parent)
3、此activity
4、此activity之後的activity
5、此activity之前的activity所調出的另外的activity。
其實這些都是虛談,
只要控制好這個activity的屬性:
android:launchMode="singleTask"
還有,當service中設置的點擊通知觸發事件:
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Bundle bundle = new Bundle();
bundle.putString("UNREAD_MESSAGE", "UNREAD_MESSAGE");
intent.putExtras(bundle);
intent.setClass(this, MessageManagementActivity.class);
(代碼沒對齊啊,大家將就點兒)
然後呢?
1、在Oncreate加入是否service通知點進來的判斷(一般通過bundle中帶有的參數判斷就好)
2、重載@Override
protected void onNewIntent(Intent intent) {}
這個函數,這是爲了處理,如果activity存在,促使其刷新的函數。