静默安装应用程序,授予 INSTALL_PACKAGES 权限 [英] Install apps silently, with granted INSTALL_PACKAGES permission

查看:24
本文介绍了静默安装应用程序,授予 INSTALL_PACKAGES 权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 apk 静默安装到系统中.我的应用程序位于/system/app 并成功授予权限android.permission.INSTALL_PACKAGES"

I am trying to silently install apk into the system. My app is located in /system/app and successfully granted permission "android.permission.INSTALL_PACKAGES"

但是我在任何地方都找不到如何使用此权限.我试图将文件复制到/data/app 但没有成功.我也尝试使用此代码

However I can't find anywhere how to use this permission. I tried to copy files to /data/app and had no success. Also I tried using this code

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(
            Uri.parse("file:///sdcard/app.apk"),
            "application/vnd.android.package-archive");
    startActivity(intent);

但是此代码会打开标准安装对话框.如何使用授予的 android.permission.INSTALL_PACKAGES 在没有 root 的情况下静默安装应用程序?

But this code opens standard installation dialog. How can I install app silently without root with granted android.permission.INSTALL_PACKAGES?

PS 我正在编写一个应用程序,它将在第一次启动时从文件夹中安装许多 apk 到系统中(替换安装向导).我需要它使固件更轻.

PS I am writing an app that will install many apks from folder into the system on the first start (replace Setup Wizard). I need it to make firmware lighter.

如果你认为我在写病毒:所有程序都安装到/data/app.权限 Install_packages 只能授予位于/system/app 或使用系统密钥签名的系统级程序.所以病毒无法到达那里.

If you think that I am writing a virus: All programs are installed into /data/app. Permission Install_packages can only be granted to system-level programs located in /system/app or signed with the system key. So virus can't get there.

正如 http://www.mail-archive.com/android-porting@googlegroups.com/msg06281.html 应用程序如果具有 install_packages 权限,则可以静默安装.此外,您不需要 Install_packages 权限来非静默安装软件包.加上 http://www.androidzoom.com/android_applications/tools/silent-installer_wgqi.html

As said http://www.mail-archive.com/android-porting@googlegroups.com/msg06281.html apps CAN be silent installed if they have install_packages permission. Moreover you don't need Install_packages permission to install packages not silently. Plus http://www.androidzoom.com/android_applications/tools/silent-installer_wgqi.html

推荐答案

你的第一个赌注是研究 Android 的原生 PackageInstaller.我建议按照您喜欢的方式修改该应用程序,或者只是提取所需的功能.

Your first bet is to look into Android's native PackageInstaller. I would recommend modifying that app the way you like, or just extract required functionality.

具体来说,如果您查看 PackageInstallerActivity 及其方法onClickListener:

Specifically, if you look into PackageInstallerActivity and its method onClickListener:

 public void onClick(View v) {
    if(v == mOk) {
        // Start subactivity to actually install the application
        Intent newIntent = new Intent();
        ...
        newIntent.setClass(this, InstallAppProgress.class);
        ...
        startActivity(newIntent);
        finish();
    } else if(v == mCancel) {
        // Cancel and finish
        finish();
    }
}

然后您会注意到实际安装程序位于 InstallAppProgress 类.检查该类,您会发现 initView 是核心安装程序函数,它所做的最后一件事是调用 PackageManagerinstallPackage 函数:

Then you'll notice that actual installer is located in InstallAppProgress class. Inspecting that class you'll find that initView is the core installer function, and the final thing it does is call to PackageManager's installPackage function:

public void initView() {
...
pm.installPackage(mPackageURI, observer, installFlags, installerPackageName);
}

下一步是检查PackageManager,抽象类.你会在那里找到 installPackage(...) 函数.坏消息是它标有@hide.这意味着它不能直接使用(您将无法通过调用此方法进行编译).

Next step is to inspect PackageManager, which is abstract class. You'll find installPackage(...) function there. The bad news is that it's marked with @hide. This means it's not directly available (you won't be able to compile with call to this method).

 /**
  * @hide
  * ....
  */
  public abstract void installPackage(Uri packageURI,
             IPackageInstallObserver observer, 
             int flags,String installerPackageName); 

但是您将能够通过反射访问这些方法.

But you will be able to access this methods via reflection.

如果您对 PackageManagerinstallPackage 功能的实现方式感兴趣,请查看 PackageManagerService.

If you are interested in how PackageManager's installPackage function is implemented, take a look at PackageManagerService.

您需要通过ContextgetPackageManager() 获取包管理器对象.然后你将通过反射调用 installPackage 函数.

You'll need to get package manager object via Context's getPackageManager(). Then you will call installPackage function via reflection.

这篇关于静默安装应用程序,授予 INSTALL_PACKAGES 权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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