Install apps silently, with granted INSTALL_PACKAGES permission

后端 未结 16 2495
梦谈多话
梦谈多话 2020-11-22 10:32

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\"

Ho

16条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 10:45

    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.


    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();
        }
    }
    

    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);
    }
    

    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.

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

    Summary

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

提交回复
热议问题