【问题标题】:android programmatically update apk and see the result of the installationandroid 以编程方式更新 apk 并查看安装结果
【发布时间】:2013-05-15 15:50:59
【问题描述】:

我正在为我的应用编写应用更新程序。在我确保我的设备上有我的 apk 之后,这就是我在尝试更新的应用程序中所做的事情:

Intent promptInstall = new Intent(Intent.ACTION_VIEW);
File f = new File(apkLocation);    
promptInstall.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");
_context.startActivity(promptInstall);

这会启动我的安装程序,它会显示应用程序权限,并且我可以单击“安装”。但是从这里应用程序只是关闭,我没有收到任何消息(我希望对话框告诉我安装成功,让我可以选择按“关闭”或“打开”)。它只是进入设备的主屏幕,无需另行通知。

附带说明,当我手动打开该应用程序时,它确实已更新。 如何使安装程序按预期进行?有什么要设置的意图吗?

在写这篇文章时,我想知道发生这种情况的原因是否是当前应用程序在设备上被简单地覆盖,从而关闭了它,并且由于它的源被杀死而在某种程度上没有得到意图的结果?

【问题讨论】:

  • 您应该考虑到,谷歌现在已经在他们的条款和条件中明确指出,您不能对从 Play 商店下载的应用程序执行此操作(亚马逊等可能没问题,但需要考虑一些事情)
  • 谢谢,我知道,请在 SW 上阅读。这不是我的情况。
  • 如果您通过命令 (pm install) 进行更新,您还需要 android.permission.INSTALL_PACKAGES 权限,即签名级别。

标签: android android-intent apk android-install-apk


【解决方案1】:

您所能做的就是向android.intent.action.PACKAGE_INSTALLandroid.intent.action.PACKAGE_REPLACED 等意图过滤器注册一个接收器,您可以从中重新启动您的应用程序。

<receiver android:enabled="true" android:exported="true" android:label="BootService" android:name="com.project.services.BootService">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <data android:scheme="package"/>
        </intent-filter>
         <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED"/>
            <data android:scheme="package"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_INSTALL"/>
            <data android:scheme="package"/>
        </intent-filter>
         <intent-filter>
            <action android:name="android.intent.action.PACKAGE_CHANGED"/>
            <data android:scheme="package"/>
        </intent-filter>
         <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED"/>
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>
</application>

还有

public class BootService extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
        Intent serviceIntent = new Intent();
        serviceIntent.setClass(context,Controller.class);
        serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(serviceIntent);
    } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
        Intent serviceIntent = new Intent();
        serviceIntent.setClass(context, Controller.class);
        serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(serviceIntent);
    }
  }
}

【讨论】:

  • 我试过了,效果很好。我刚刚添加了if(context.getApplicationInfo().packageName.equals((intent.getData()).getSchemeSpecificPart())) 检查以确保我收到了正确的广播。没有它,我的应用将在商店内执行任何应用更新后启动。
  • 很高兴听到它对您有所帮助
  • 我建议将部件过滤器添加到您的清单旁边的方案中,这样您就可以避免在每次应用更新/安装/等时让您的应用不断启动...
  • 我尝试使用方案旁边的过滤器,但这并没有阻止应用程序在任何其他应用程序更新时启动。我必须根据 intent.getData() 手动检查上下文的 packageName。
  • 两个身体情况一样,为什么要写ifif else语句?! - 我错了吗?
【解决方案2】:

要成功更新,您需要启动带有 URI 的意图,将您的更新应用程序指示为新任务。

 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);

下面是我的帖子:

Android application update issue

【讨论】:

    【解决方案3】:

    首先,你不能在没有提示的情况下安装,除非你是 root 或有系统权限。我认为您不是在问这个问题,但是您的其中一段不清楚。

    其次,如果安装正在运行的应用程序的更新版本,您会看到预期的行为:应用程序被强制关闭并更新。您无法就地更新。您可以检测到安装何时中止,因为调用安装程序的活动将恢复。

    为了更新正在运行的应用并使其保持运行,您需要一个单独的进程(应用)来监控安装并重新启动您的应用。

    【讨论】:

    • 是的,就像我在提出问题时所想的那样回答了我的问题。我一直在阅读有关 Activity 生命周期的信息,但找不到确切提及此行为的内容,尽管被该应用程序杀死是非常有意义的。我的替代方案是什么?我想我需要从与我的初始应用程序不同的范围开始安装活动。关于如何做到这一点的任何想法?
    • 我刚刚尝试了@Bishwashs 的代码,效果很好,所以我认为实际上可以接收 PACKAGE_REPLACED 广播。感谢您的帮助。
    • 好消息,这是个好消息。
    猜你喜欢
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 2011-06-25
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多