Android開發:友盟推送多次重複打開頁面,退出時得多次退出才能退出的解決辦法(淺顯方法)
(---寫的有點囉嗦,請諒解)
在做友盟推送的時候,遇到了標題這個問題,當時初始加入時,本來打算直接就不解決這個問題,操作上就複雜一點點拉到了。到後來還是解決了,客戶使用太麻煩,有厭惡感。
bug詳情:友盟推送時:每次接收一條推送信息,打開時就相當於啓動了一個應用;在不關閉的情況下,又來一個推送的信息,又打開一下,又開了一個應用;·········開了10個以上的話,退出時就得退十次。這樣用戶明顯會感覺不舒服的。
bug分析:這是和task的啓動模式有關,但是也沒有太大關係;每次啓動都是啓動一個新的task(相當於啓動一個新應用),導致最終啓動了好多個,關閉得一個一個關,而且之間也沒有聯繫,也不能通過startActivityForResult()方法接收。
bug解決思路:我的想法是:我們採用home鍵的思路---點擊home鍵程序就會到後臺運行,那樣就只需要點擊一次就可以讓程序在後臺運行。帶着這樣的想法,我就開始了設計此次解決的方法。
bug解決方法:1.退出時點擊確定然後執行“home鍵”功能:---相當於後臺運行的功能
代碼如下://相當於home鍵的功能
Intent i = new Intent(Intent.ACTION_MAIN);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addCategory(Intent.CATEGORY_HOME);
mactivity.startActivity(i);
mactivity.finish();
2.在這種情況下,第二次進入app時,會直接顯示後臺數據,此時若再次退出的話,直接就退出了。
3.關於是否使用task(堆棧)功能:
基本沒有使用:代碼如下:3-1
/**
* //這邊不需要這麼複雜,直接下面那樣即可
* Intent intent = new Intent(Intent.ACTION_MAIN);//Intent.ACTION_MAIN
intent.addCategory(Intent.CATEGORY_LAUNCHER);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
| Intent.FLAG_ACTIVITY_CLEAR_TOP);// 關鍵的一步,設置啓動模式
intent.setClass(context, LoginActivity.class);
startActivity(intent);
*/
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(context, LoginActivity.class);
startActivity(intent);
xml配置:3-2
<!-- application這裏面增加不增加 啓動模式:android:launchMode="singleTask" 都無所謂-->
<application
android:name="com.ynd.struct.publicClass"
android:allowBackup="true"
android:icon="@drawable/ioc3"
android:label="@string/app_name"
android:logo="@drawable/ioc3"
android:theme="@style/CustomAppTheme" >
<!-- 如果LoginActivity想能夠啓動,這裏面一定不能增加啓動模式:android:launchMode="singleTask";加了就不能正常啓動 -->
<activity
android:name="com.ynd.main.LoginActivity"
android:label="@string/title_activity_login"
android:theme="@style/CustomAppTheme" >
<!-- android:launchMode="singleTask" -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
總結:基本就是這樣解決的。有錯誤請指正,但我感覺這個有點不正統。