android以编程方式更新apk并查看安装结果 [英] android programmatically update apk and see the result of the installation

查看:24
本文介绍了android以编程方式更新apk并查看安装结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing an app updater for my app. After I make sure I have my apk on the device, this is what I do from within the app I'm trying to update:

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

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

This launches my installer which displays the app permissions and I am able to click "Install". But from here the app simply closes, I get no message whatsoever (I would've expected the dialog telling me the install was successful giving me the option to press "Close" or "Open"). It just goes to the main screen of the device without further notice.

顺便提一下,当我手动打开应用程序时,它确实更新了.如何让安装程序按预期进行?有什么要设置的意图吗?

On a side note, the app is indeed updated when I manually open it back. How can I make the installer go all the way as expected? Is there anything to set on the intent?

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

While writing this, I'm wondering if the reason this happens is that the current app is simply overwritten on the device thus closing it and by extent not getting the result of the intent because it's source was killed?

推荐答案

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

All you can do is register a receiver with the intent filters like android.intent.action.PACKAGE_INSTALL or android.intent.action.PACKAGE_REPLACED from which you can restart your application back again.

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

这篇关于android以编程方式更新apk并查看安装结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆