以编程方式安装/卸载APK(PackageManager vs Intents)

时间:2011-07-25 08:17:31

标签: android android-intent install uninstall apk

我的应用程序安装其他应用程序,它需要跟踪它已安装的应用程序。当然,这可以通过简单地保留已安装的应用程序列表来实现。但这不应该是必要的! PackageManager应该负责维护installedBy(a,b)关系。事实上,根据API,它是:

public abstract String getInstallerPackageName (String packageName) - 检索安装包的应用程序的包名称。这确定了包裹来自哪个市场。

当前的方法

使用Intent安装APK

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
startActivity(intent);

使用Intent卸载APK:

Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package",
getPackageManager().getPackageArchiveInfo(apkUri.getPath(), 0).packageName,null));
startActivity(intent);

这显然不是例如Android Market安装/卸载软件包。他们使用更丰富的PackageManager版本。通过从Android Git存储库下载Android源代码可以看到这一点。以下是与Intent方法相对应的两种隐藏方法。不幸的是,外部开发人员无法使用它们。但也许他们将来会在这里?

更好的方法

使用PackageManager安装APK

/**
 * @hide
 * 
 * Install a package. Since this may take a little while, the result will
 * be posted back to the given observer.  An installation will fail if the calling context
 * lacks the {@link android.Manifest.permission#INSTALL_PACKAGES} permission, if the
 * package named in the package file's manifest is already installed, or if there's no space
 * available on the device.
 *
 * @param packageURI The location of the package file to install.  This can be a 'file:' or a
 * 'content:' URI.
 * @param observer An observer callback to get notified when the package installation is
 * complete. {@link IPackageInstallObserver#packageInstalled(String, int)} will be
 * called when that happens.  observer may be null to indicate that no callback is desired.
 * @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
 * {@link #INSTALL_REPLACE_EXISTING}, {@link #INSTALL_ALLOW_TEST}.
 * @param installerPackageName Optional package name of the application that is performing the
 * installation. This identifies which market the package came from.
 */
public abstract void installPackage(
        Uri packageURI, IPackageInstallObserver observer, int flags,
        String installerPackageName);

使用PackageManager卸载APK

/**
 * Attempts to delete a package.  Since this may take a little while, the result will
 * be posted back to the given observer.  A deletion will fail if the calling context
 * lacks the {@link android.Manifest.permission#DELETE_PACKAGES} permission, if the
 * named package cannot be found, or if the named package is a "system package".
 * (TODO: include pointer to documentation on "system packages")
 *
 * @param packageName The name of the package to delete
 * @param observer An observer callback to get notified when the package deletion is
 * complete. {@link android.content.pm.IPackageDeleteObserver#packageDeleted(boolean)} will be
 * called when that happens.  observer may be null to indicate that no callback is desired.
 * @param flags - possible values: {@link #DONT_DELETE_DATA}
 *
 * @hide
 */
public abstract void deletePackage(
        String packageName, IPackageDeleteObserver observer, int flags);

差异

  • 使用意图时,本地包管理器不知道安装源自哪个应用程序。具体来说,getInstallerPackageName(...)返回null。

  • 隐藏方法installPackage(...)将安装程序包名称作为参数,并且很可能能够设置此值。

问题

是否可以使用意图指定包安装程序名称? (也许安装程序包的名称可以作为安装意图的额外添加?)

提示:如果您要下载Android源代码,可以按照此处描述的步骤操作:下载源树。要提取* .java文件并根据包层次结构将它们放在文件夹中,您可以查看这个简洁的脚本:View Android Source Code in Eclipse

10 个答案:

答案 0 :(得分:67)

[卸载]

怎么样:

Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:com.example.mypackage"));
startActivity(intent);

用于卸载。似乎更容易......

答案 1 :(得分:66)

目前第三方应用程序无法使用此功能。请注意,即使使用反射或其他技巧来访问installPackage()也无济于事,因为只有系统应用程序才能使用它。 (这是因为它是低级别的安装机制,在用户批准了权限之后,因此常规应用程序无法安全访问。)

此外,平台版本之间的installPackage()函数参数经常发生变化,因此您尝试访问它的任何内容都将在平台的各种其他版本上失败。

编辑:

另外值得指出的是,这个installerPackage最近才被添加到平台(2.2?),并且最初并未用于跟踪安装应用程序的用户 - 平台使用它来确定何时启动报告应用程序的错误,以实现Android反馈。 (这也是API方法参数发生变化的次数之一。)至少在推出它之后很长一段时间,Market仍然没有使用它来跟踪它已经安装的应用程序(它可能仍然没有使用它),而只是用它来设置Android反馈应用程序(与市场分开)作为“所有者”来处理反馈。

答案 2 :(得分:36)

API级别14引入了两项新操作:ACTION_INSTALL_PACKAGEACTION_UNINSTALL_PACKAGE。这些操作允许您传递EXTRA_RETURN_RESULT boolean extra以获取(联合国)安装结果通知。

调用卸载对话框的示例代码:

String app_pkg_name = "com.example.app";
int UNINSTALL_REQUEST_CODE = 1;

Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);  
intent.setData(Uri.parse("package:" + app_pkg_name));  
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, UNINSTALL_REQUEST_CODE);

并使用Activity#onActivityResult方法接收通知:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == UNINSTALL_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Log.d("TAG", "onActivityResult: user accepted the (un)install");
        } else if (resultCode == RESULT_CANCELED) {
            Log.d("TAG", "onActivityResult: user canceled the (un)install");
        } else if (resultCode == RESULT_FIRST_USER) {
            Log.d("TAG", "onActivityResult: failed to (un)install");
        }
    }
}

答案 3 :(得分:22)

如果您拥有设备所有者(或个人资料所有者,我没有尝试过)权限,您可以使用设备所有者API静默安装/卸载软件包。

用于卸载:

public boolean uninstallPackage(Context context, String packageName) {
    ComponentName name = new ComponentName(MyAppName, MyDeviceAdminReceiver.class.getCanonicalName());
    PackageManager packageManger = context.getPackageManager();
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        PackageInstaller packageInstaller = packageManger.getPackageInstaller();
        PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
                PackageInstaller.SessionParams.MODE_FULL_INSTALL);
        params.setAppPackageName(packageName);
        int sessionId = 0;
        try {
            sessionId = packageInstaller.createSession(params);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        packageInstaller.uninstall(packageName, PendingIntent.getBroadcast(context, sessionId,
                new Intent("android.intent.action.MAIN"), 0).getIntentSender());
        return true;
    }
    System.err.println("old sdk");
    return false;
}

并安装包:

public boolean installPackage(Context context,
                                     String packageName, String packagePath) {
    ComponentName name = new ComponentName(MyAppName, MyDeviceAdminReceiver.class.getCanonicalName());
    PackageManager packageManger = context.getPackageManager();
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        PackageInstaller packageInstaller = packageManger.getPackageInstaller();
        PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
                PackageInstaller.SessionParams.MODE_FULL_INSTALL);
        params.setAppPackageName(packageName);
        try {
            int sessionId = packageInstaller.createSession(params);
            PackageInstaller.Session session = packageInstaller.openSession(sessionId);
            OutputStream out = session.openWrite(packageName + ".apk", 0, -1);
            readTo(packagePath, out); //read the apk content and write it to out
            session.fsync(out);
            out.close();
            System.out.println("installing...");
            session.commit(PendingIntent.getBroadcast(context, sessionId,
                    new Intent("android.intent.action.MAIN"), 0).getIntentSender());
            System.out.println("install request sent");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    System.err.println("old sdk");
    return false;
}

答案 4 :(得分:4)

访问这些方法的唯一方法是通过反射。您可以通过调用PackageManager并使用反射访问这些方法来获取getApplicationContext().getPackageManager()对象的句柄。结帐this教程。

答案 5 :(得分:3)

根据Froyo源代码,将在PackageInstallerActivity中查询Intent.EXTRA_INSTALLER_PACKAGE_NAME附加密钥以获取安装程序包名称。

答案 6 :(得分:2)

在root设备上,您可以使用:

String pkg = context.getPackageName();
String shellCmd = "rm -r /data/app/" + pkg + "*.apk\n"
                + "rm -r /data/data/" + pkg + "\n"
                // TODO remove data on the sd card
                + "sync\n"
                + "reboot\n";
Util.sudo(shellCmd);

Util.sudo() is defined here.

答案 7 :(得分:1)

如果您将包名称作为参数传递给任何用户定义的函数,请使用以下代码:

    Intent intent=new Intent(Intent.ACTION_DELETE);
    intent.setData(Uri.parse("package:"+packageName));
    startActivity(intent);

答案 8 :(得分:0)

如果您正在使用Kotlin,API 14+,并希望为您的应用显示卸载对话框:

startActivity(Intent(Intent.ACTION_UNINSTALL_PACKAGE).apply {
    data = Uri.parse("package:$packageName")
})

如果要提示用户卸载设备上的其他应用,可以将packageName更改为任何其他包名称

答案 9 :(得分:0)

<强>先决条件:

您的APK需要由系统签名,如前所述。实现这一目标的一种方法是自己构建AOSP映像并将源代码添加到构建中。

<强>代码:

作为系统应用安装后,您可以使用包管理器方法安装和卸载APK,如下所示:

安装:

public boolean install(final String apkPath, final Context context) {
    Log.d(TAG, "Installing apk at " + apkPath);
    try {
        final Uri apkUri = Uri.fromFile(new File(apkPath));
        final String installerPackageName = "MyInstaller";
        context.getPackageManager().installPackage(apkUri, installObserver, PackageManager.INSTALL_REPLACE_EXISTING, installerPackageName);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

卸载:

public boolean uninstall(final String packageName, final Context context) {
    Log.d(TAG, "Uninstalling package " + packageName);
    try {
        context.getPackageManager().deletePackage(packageName, deleteObserver, PackageManager.DELETE_ALL_USERS);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

要在安装/卸载APK后进行回叫,您可以使用:

/**
 * Callback after a package was installed be it success or failure.
 */
private class InstallObserver implements IPackageInstallObserver {

    @Override
    public void packageInstalled(String packageName, int returnCode) throws RemoteException {

        if (packageName != null) {
            Log.d(TAG, "Successfully installed package " + packageName);
            callback.onAppInstalled(true, packageName);
        } else {
            Log.e(TAG, "Failed to install package.");
            callback.onAppInstalled(false, null);
        }
    }

    @Override
    public IBinder asBinder() {
        return null;
    }
}

/**
 * Callback after a package was deleted be it success or failure.
 */
private class DeleteObserver implements IPackageDeleteObserver {

    @Override
    public void packageDeleted(String packageName, int returnCode) throws RemoteException {
        if (packageName != null) {
            Log.d(TAG, "Successfully uninstalled package " + packageName);
            callback.onAppUninstalled(true, packageName);
        } else {
            Log.e(TAG, "Failed to uninstall package.");
            callback.onAppUninstalled(false, null);
        }
    }

    @Override
    public IBinder asBinder() {
        return null;
    }
}

/**
 * Callback to give the flow back to the calling class.
 */
public interface InstallerCallback {
    void onAppInstalled(final boolean success, final String packageName);
    void onAppUninstalled(final boolean success, final String packageName);
}