在 android 上以编程方式安装 APK [英] Install APK programmatically on android

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

问题描述

我一直在尝试让 Android 应用程序以编程方式在 sdcard 上安装 APK,但遇到了一些麻烦.

I've been trying to get an android application to install an APK on the sdcard programmatically but I'm running into a little trouble.

我是这样做的:

Intent intent = new Intent(Intent.ACTION_VIEW);           
intent.setDataAndType("ApkFilePath...","application/vnd.android.package-archive");
activity.startActivityForResult(intent,5000);

现在一切正常,它带来了包管理器,我可以控制当管理器安装完 APK 后要做什么.

Now that works ok, it brings the package manager and I can control what to do when the manager finishes installing the APK.

但我遇到的问题是,如果在安装结束时用户单击打开"而不是完成",则不会调用OnActivityResult"方法,因为管理器仍然存在......这对系统的另一个要求提出了另一个问题.

But the issue that I'm having is that if at the end of the installation the user clicks on "Open" instead of "Done" the "OnActivityResult" method is not called, as the manager still exists.... and this presents another issue on another requirement on the system.

有没有办法知道用户何时在包管理器的末尾选择了打开",或者有没有办法强制管理器只显示我希望它显示的按钮?

Is there a way to know when the user has selected "Open" at the end of the package manager, or is there a way to force the manager to display only the buttons I want it to display?

真的可以使用帮助,我到处搜索,似乎没有找到解决方案

Really could use the help, I've search everywhere and don't seem to find a solution

推荐答案

如果安装了新应用,您可以将接收器添加到 AndroidManifest.xml 以收听广播.像这样:

You can add a receiver to your AndroidManifest.xml to listen to broadcasts if a new app is installed. Like this:

<receiver android:name=".PackageReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="package" />
    </intent-filter>
</receiver>

然后在安装新包时调用这个类:

This class then gets called when a new package is installed:

public class PackageReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    // handle install event here
  }
}

这篇关于在 android 上以编程方式安装 APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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