Android Silent Installation
In general, the Android system will display an installation interface for installing the apk. You can click OK or cancel to install the apk.
However, in actual project requirements, there is a requirement that you want to install apk in the background (without the installation interface prompt). This installation method is called Silent Installation.
There are many ways to implement Silent Installation for Android. Here we only introduce a simple and easy-to-understand method, that is, to call the unpublished API (installPackage) of Android for implementation.
The following is a simple Demo to implement silent installation. The interface is as follows:
When you click the "Silent Installation" button, the apk of the 360 mobile guard will be installed in the background. After the installation is successful, the Toast prompt will pop up, indicating that the installation is successful.
First, declare the following permissions in AndroidManifest. xml:
The main implementation code is as follows:
It can be seen from the above that an apk that requires Silent Installation needs to be placed on the SD card in advance (this apk is not detected, but is simply implemented)
The preceding method involves the PackageInstallObserver class. The implementation code is as follows:
The Code has been basically implemented, but in eclipse, this Demo is not compiled, because the installPackage API is not public and cannot be called at the upper layer, you need to compile the SDK In the Android source code environment and create an Android SDK in the Demo root directory. mk file with the following content:
Use the mk file in the source code.
The following describes the meanings of mk options:
The following headers and tails are fixed:
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
......
LOCAL_SRC_FILES: = $ (call all-subdir-java-files)
......
Include $ (BUILD_PACKAGE)
LOCAL_MODULE_TAGS: = user eng tests optionaluser: This module is compiled only in the user version. eng: This module is compiled only in the eng version. tests: indicates that this module is compiled only in the tests version.
Optional: indicates the name of the project to be compiled by this module in all versions.
If your APK depends on a third-party jar package, you can use the following options to define LOCAL_STATIC_JAVA_LIBRARIES: = xsocket jackson-mapper logging jackson-core javatar log4j
Android. mk has many shampoo and specifications. You can check the information on the Internet.
After preparation, You can compile the Demo using mmm In the Android source code environment. Of course, you need to configure the Source Code compiling environment by yourself.