您的位置:首页 > 移动开发 > Android开发

Android开发:友盟推送多次重复打开页面,退出时得多次退出才能退出的解决办法(浅显方法)

2015-09-23 20:37 447 查看
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>

总结:基本就是这样解决的。有错误请指正,但我感觉这个有点不正统。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: