启动器应用程序如何在后台卸载应用程序?

时间:2018-06-16 09:44:03

标签: android android-intent uninstall

背景

我有一个空闲时间应用程序,其主要功能之一是轻松卸载应用程序。

问题

我使用非常基本的API来卸载应用,对于用户来说,这似乎是两个步骤:确认卸载,并看到一些显示卸载过程的对话框。

我注意到在某些启动器应用程序(例如Nova启动程序和Pixel Launcher)上,卸载后,它们只显示了一步,即确认卸载所选应用程序。

以下是通过我的应用以及内置的app-info屏幕的外观。首先你看到了这个:

enter image description here

然后这个:

enter image description here

但是,在发射器上,你会看到这个:

enter image description here

不久之后,应用程序就消失了,而你之间没有看到任何东西。

我发现了什么

我知道在后台卸载应用程序的方法是使用root,但这不是那些应用程序所做的。

这是我用于正常卸载程序的内容:

public static Intent prepareUninstallAppIntent(@NonNull final Context context, @NonNull final String packageName) {
    final Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, Uri.fromParts("package", packageName, null));
    intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
    intent.putExtra("android.intent.extra.UNINSTALL_ALL_USERS", true);
    return null;
}

问题

  1. 启动器应用程序如何仅显示确认对话框,并在后台执行卸载?

  2. 这是一个新的API吗?如果是这样,从哪个版本?我在哪里可以阅读它?

  3. 编辑:这不再重要了。看看它是如何工作的。

    我现在通过查看Lawnchair启动器应用代码发现,它与我所做的有点相似:

    public static boolean startUninstallActivity(
            final Launcher launcher, ItemInfo info, DropTargetResultCallback callback) {
        Pair<ComponentName, Integer> componentInfo = getAppInfoFlags(info);
        ComponentName cn = componentInfo.first;
    
        final boolean isUninstallable;
        if ((componentInfo.second & AppInfo.DOWNLOADED_FLAG) == 0) {
            // System applications cannot be installed. For now, show a toast explaining that.
            // We may give them the option of disabling apps this way.
            Toast.makeText(launcher, R.string.uninstall_system_app_text, Toast.LENGTH_SHORT).show();
            isUninstallable = false;
        } else {
            Intent intent = new Intent(Intent.ACTION_DELETE,
                    Uri.fromParts("package", cn.getPackageName(), cn.getClassName()))
                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            intent.putExtra(Intent.EXTRA_USER, info.user);
            launcher.startActivity(intent);
            isUninstallable = true;
        }
        if (callback != null) {
            sendUninstallResult(
                    launcher, isUninstallable, componentInfo.first, info.user, callback);
        }
        return isUninstallable;
    }
    

    它与其他发射器的工作方式相同,所以,对我来说,似乎他们为当前用户添加了卸载部分(使用android.os.Process.myUserHandle();获取)。

    但我还是试过了。似乎工作,所以我尝试删除越来越多的代码,直到我得到的东西没有奇怪的东西。

    这是最小的代码:

        final Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, Uri.fromParts("package", packageName, null));
    

    你只需使用startActivity,而不是startActivityForResult。

    是。这很简短,我想知道我多久没有使用它......

    但现在我还有其他问题:

    1. cn.getClassName()Intent.EXTRA_USER用于什么?

    2. 此代码将从哪个版本的Android中在后台卸载应用程序?

    3. ACTION_UNINSTALL_PACKAGE和ACTION_DELETE之间有什么区别吗?

    4. 我删除了所有标志,它似乎仍然执行相同的工作,没有历史记录中的任务。他们真的需要吗?

    5. 有更多方法可以卸载应用吗?也许是批量生产?

0 个答案:

没有答案