Install, uninstall, and update Android programs

Source: Internet
Author: User

Author: Xu jianxiang (netpirate@gmail.com)
Date: 2010/07/28
Web: http://www.anymobile.org

 

Installer: software from scratch.

Uninstall program: software from scratch.

Update Program: overwrite and install the software. The data in the original version can be retained to improve the software version.

 

Installation Method:

 

1,
The Intent mechanism is used to call up the system to install the application. If you reinstall the application, the data of the original application will be retained.

 

String fileName =
Environment. getExternalStorageDirectory () +ApkName;

Uri uri = Uri. fromFile (NewFile (fileName ));

Intent intent =NewIntent (Intent.ACTION_VIEW);

Intent. setDataAndType (Uri, application/vnd. android. package-archive ");

StartActivity (intent );

 

2,
Directly call the installation interface.

 

Uri mPackageURI = Uri.FromFile(NewFile (Environment.GetExternalStorageDirectory()
+ApkName));

IntInstallFlags = 0;

PackageManager pm =
GetPackageManager ();

Try

{

PackageInfo pi = pm. getPackageInfo (PackageName,

PackageManager.GET_UNINSTALLED_PACKAGES);

If(Pi! =Null)

{

InstallFlags | = PackageManager.REPLACE_EXISTING_PACKAGE;

}

}

Catch(NameNotFoundException e)

{}

PackageInstallObserver observer
=NewPackageInstallObserver ();

Pm. installPackage (mPackageURI, observer, installFlags );

 

Application installation permission:Android. permission. INSTALL_PACKAGES

 

This method can be used for system applications (installed under/system/app). Third-party applications cannot apply for the installation and uninstallation permission.

Java. lang. SecurityException: Neither user
10039 nor current process has android. permission. INSTALL_PACKAGES.

 

3,
Run the install command.

 

Install-r is updated and installed by default. If the-r parameter is not attached, the data of the original application is clear. if the version is consistent, the installation fails.

(1) am start...

(2)Runtime.exe c (String [] args)

(3) Class <?> ExecClass =
Class. forName ("android. OS. Exec ");

 

4,
Run the cp/adb push command.

 

The system detects updates to the application and automatically reinstalls the application.

 

5,
Implemented through third-party software.

 

Market, EOE, and eTrackDog all adopt the first method for update.

Advantage: the installation program is controlled by the system's core applications;

Disadvantage: the installation process cannot be controlled. After the installation is complete, the application cannot be started immediately. You need to confirm the installation and the application cannot be expanded.

 

Instance: Market find Installer

Intent intent =

New Intent (Intent. ACTION_VIEW,
Uri. parse ("market: // search? Q = pname: your. app. id "));

StartActivity (intent );

 

How to uninstall a program:

 

1,
The Intent mechanism is used to call up the system to uninstall the application.

Uri packageURI = Uri. parse ("package: your. app. id ");

Intent intent =NewIntent (Intent.ACTION_DELETE);

StartActivity (intent );

 

2,
Directly call the uninstall interface.

 

PackageInstallObserver observer
=NewPackageInstallObserver ();

Pm. installPackage (mPackageURI, observer, installFlags );

 

Uninstall application permissions:Android. permission. DELETE_PACKAGES

 

3,
Run the rm apk installation file, which is called by the system to uninstall the application after detection.

 

Note:

For Android application installation, there is an item in the system settings to check whether an unknown source is installed. When the software is updated, check this option. If this option is checked, only the installer provided by Market source is allowed to be installed. If no setup is checked, the system will prompt the user to set the installation. If you select the setup, the installation interface cannot be returned after the setup is completed; if you choose to cancel, the installation program will be released. Therefore, if it is an update, you must check the settings of the licensed installation source before downloading, or check whether a new installer has been downloaded before downloading to avoid repeated downloading.

 

The related code is as follows:

1.
IntResult = Settings. Secure. getInt (getContentResolver (), Settings. Secure. INSTALL_NON_MARKET_APPS, 0 );

2.
If(Result = 0 ){

3.
// Show some dialog here

4.
//...

5.
// And may be show application settings dialog manually

6.
Intent intent =NewIntent ();

7.
Intent. setAction (Settings. ACTION_APPLICATION_SETTINGS );

8.
StartActivity (intent );

9.
}

 

Public static final
Class Settings. Secure extends Settings. NameValueTable

Public static final StringINSTALL_NON_MARKET_APPS

Since: API Level 3

Whether the package
Installer shocould allow installation of apps downloaded from sources other
The Android Market (vending machine). 1 = allow installing from other sources 0
= Only allow installing from the Android Market.

 

The following describes how to update a program:






OVER!

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.