Android应用程序更新问题

daw*_*ski 8 android intentfilter android-intent android-activity

最近我一直面临着我的Android应用更新过程的问题.

简而言之,应用程序能够检查是否在服务器上上载了更高版本代码的更新.如果是,则用户决定是否更新.加载该应用程序并开始标准安装后:

final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(PATH_TO_APK)), "application/vnd.android.package-archive");
startActivity(intent)
Run Code Online (Sandbox Code Playgroud)

问题是当android Intent完成安装时,"理论上"活动信息"Application was installed"和2个按钮"Done","Open".我写了"理论上",因为到目前为止我遇到过以下情况:

  1. 安装了应用程序,显示"已安装应用程序"消息的活动,用户单击"打开"但没有任何反应(Android 2.3.*)或应用程序确实正确打开 - 此行为是随机的.

  2. 已安装应用程序,显示"已安装应用程序"消息的活动但突然消失.

试图绕过这个bug(?)我找到了http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED.我实现的BroadcastReceiver启动了Launch Activity,让我们说这是一个合适的解决方案.

         <receiver android:name=\".MyReceiver\" >
          <intent-filter>
              <action android:name="android.intent.action.ACTION_PACKAGE_REPLACED" />
              //Or from API 12 <action android:name="android.intent.action.ACTION_MY_PACKAGE_REPLACED" />
           </intent-filter>
         </receiver>
Run Code Online (Sandbox Code Playgroud)

必须修改此解决方案,因为具有较低API(低于12)的应用程序无法处理ACTION_MY_PACKAGE_REPLACED,因此我实现了依赖于API的行为:

  • 允许正常安装udpate app并使用"Done"/"Open"按钮从Activity启动应用程序(API <12)

  • 在ACTION_MY_PACKAGE_REPLACED注意到后,通过MyReceiver启动了更新应用程序.

这是我目前的解决方案.

我的问题是:

  • 为什么更新的应用程序在安装到Android的API低于12后单击"打开"后随机打开?

  • 为什么带有"Done"/"Open"按钮的活动会在具有更高API的设备上消失?

我试图在安装之前完成应用程序,但它没有帮助.

我的解释是,在安装过程之后,新包必须覆盖旧包,因此必须简单地删除旧包,这是消除启动活动的主要原因.

正如我写的,这是我目前的解决方案,我不满意.如果有人能澄清此事,我将非常感激.

谢谢阅读.

编辑:

好的,解决方案非常简单:要成功更新,您需要启动Intent作为新任务(arrrgh ...):

final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(PATH_TO_APK)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

And*_*w.T 0

首先,在清单中添加“intent-filter”,如下所示:

`<activity android:name="com.package.MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="file" />
        <data android:mimeType="application/vnd.android.package-archive" />
    </intent-filter>
</activity>`
Run Code Online (Sandbox Code Playgroud)

然后,为新任务设置意图标志:

intentAPK.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);