编程的android apk文件更新和查看安装的结果 [英] android programmatically update apk and see the result of the installation

查看:119
本文介绍了编程的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);

这将启动我安装它显示应用程序的权限,我可以点击安装。但是,从这里应用程序直接关闭,我无论如何得不到消息(我会一直期待的对话框,告诉我安装成功,让我有选择preSS关闭或打开)。它只是到设备的主屏幕,恕不另行通知。

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_INSTALL android.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天全站免登陆