Android software package Silent Installation of small applications-source code

Source: Internet
Author: User

Early in the morning, I wrote an android software package for Silent Installation, which has been put in the workspace for a long time. Today I found it when I sorted out the project and took it out for the sun!
 
Features implemented by the software: When silent installation is required (that is, the installation confirmation dialog box is not displayed and the background is automatically installed), start the installation service and place the path of the software package to intent, start the installation service. After the software is successfully installed, a broadcast will be sent. You only need to accept the corresponding broadcast to check whether the software is successfully installed!
 
Key software code:
Java code
1. class PackageInstallObserver extends IPackageInstallObserver. Stub {
2. public void packageInstalled (String packageName, int returnCode ){
3. Intent intent = new Intent (Constants. ACTION_INSTALLED );
4. intent. putExtra (Constants. EXTRA_INSTALL_RESULT, returnCode );
5. intent. putExtra (Constants. EXTRA_PACKAGE_NAME, packageName );
6. String apkName = map. get (packageName );
7. if (apkName! = Null) intent. putExtra (Constants. EXTRA_APK_NAME, apkName );
8. mContext. sendBroadcast (intent );
9 .}
10 .}
This class is probably an observer. When the installation is successful or fails, the packageInstalled method of this class is called. Therefore, we can write the response code for successful or failed installation here. The above class is to send a broadcast to inform others that the software is successfully installed.
 
Java code
1. public void install (String apkPath, String apkName ){
2. File file = new File (apkPath );
3. if (! File. exists ())
4. return;
5. Uri mPackageURI = Uri. fromFile (file );
6. int installFlags = 0;
7. PackageManager pm = getPackageManager ();
8. PackageInfo info = pm. getPackageArchiveInfo (apkPath, PackageManager. GET_ACTIVITIES );
9. if (info! = Null ){
10. try {
11. PackageInfo pi = pm. getPackageInfo (info. packageName, PackageManager. GET_UNINSTALLED_PACKAGES );
12. if (pi! = Null ){
13. installFlags | = PackageManager. INSTALL_REPLACE_EXISTING;
14 .}
15.} catch (NameNotFoundException e ){
16 .}
17. // match the package name with the apkName.
18. map. put (info. packageName, apkName );
19. IPackageInstallObserver observer = new PackageInstallObserver ();
20. pm. installPackage (mPackageURI, observer, installFlags, info. packageName );
21 .}
22 .}
This method is the key software package installation method. First, check whether the given software installation package exists. The subsequent code is to determine whether the system has installed the software. If the software has been installed, the installation parameters are replaced.
Pm. installPackage (mPackageURI, observer, installFlags, info. packageName );
This line of code calls PackageManager's installPackage method to install the software package and registers an observer.
 
Another way to install the software package is to send an intent request for the system to install the software package, so we cannot control the installation prompt interface. This method directly calls the system's core method and skips the installation confirmation interface to implement Silent Installation.
 
 
Analysis, because this api is a system shadow api, an error will be reported if it is called directly. Therefore, this application must be compiled in the source code environment. For details about how to build the key source code environment, refer to the blog: http://www.bkjia.com/kf/201204.
In addition, because the system hidden api is called, this application must be installed in the system app directory before it can be called successfully. Therefore, this method is suitable for the following requirements: first, it is suitable for built-in rom, second, or obtaining the root permission of the system, and then copying the app to the system/app directory, and set the permissions. Now the android mobile phone basically has the root permission, which is not hard to achieve.
 
I hope I can provide some help to people in need ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.