Android installation and uninstall APK

Source: Internet
Author: User

String filename = "/sdcard/testb.apk ";
Intent intent = new intent (intent. action_view );
Intent. setdataandtype (URI. parse ("file: //" + new file (filename), "application/vnd. Android. Package-Archive ");

Startactivity (intent );

 

 

 

1. InstallationAPK
String dirpath = "/data/" + getpackagename () + "/files/test.APK"; // The file must have the read permission
Intent intent = new intent ();
Intent. setaction (Android. content. Intent. action_view );
Intent. setdataandtype (URI. parse ("file: //" + filepath), "application/vnd. Android. Package-Archive ");
Intent. setflags (intent. flag_activity_new_task );
Startactivity (intent );

2. UninstallAPK
Private void startuninstall (final string PKG ){
If (! Installutils. isapkinstalled (mcontext, PKG )){
Toast. maketext (mcontext, "the program is not installed. You do not need to uninstall it! ", Toast. length_short). Show ();
Return;
} Else {
Uri packageuri = URI. parse ("package:" + PKG );
Intent uninstallintent = new intent (intent. action_delete, packageuri );
Startactivity (uninstallintent );
}
}

3. Run
Intent to = new intent ();
To. putextra ("TOP", "Hello, from upper layer information ");
To. setclassname (pkgname, clsname );
To. setflags (intent. flag_activity_new_task );
Startactivity ();

4. Tools
Installutils. Java public static Boolean isapkinstalled (context, final string pkgname ){
Try {
Context. getpackagemanager (). getpackageinfo (pkgname, 0 );
Return true;
} Catch (namenotfoundexception e ){
// E. printstacktrace ();
}
Return false;

5. Set the portal class of the installed program androidmanifest. XML:
<Intent-filter>
<Action Android: Name = "android. Intent. Action. View"/>
<Category Android: Name = "android. Intent. Category. Default"/>
</Intent-filter>NoThe application will appear on the desktopStartIcon.

6,StartAny otherAPKAny activity in.
LikeStartIntentfilter:
<Intent-filter>
<Action Android: Name = "android. Intent. Action. View"/>
<Category Android: Name = "android. Intent. Category. Default"/>
</Intent-filter>

Application installation is the main feature of a smart machine. Users can install various applications (such as games) on their mobile phones and perform management operations such as uninstallation. APK is the abbreviation of Android package, that is, the android installation package. APK is a file format similar to Symbian sis Or sisx. Directly upload the APK file to the android simulator or Android mobile phone for installation.

Android applications can be installed in the following four ways:

1. system application installation-completed at startup, no installation Interface

2. download and install the application over the Internet-the application is completed through the market application, and there is no installation Interface

3. Install the ADB tool-no installation interface.

4. The third party shall install zookeeper through the SDK-based APK file. The packageinstaller.apk application shall process the installation and uninstallation interfaces.

Application installation process and Path
Application installation involves the following directories:

System/APP

Applications that come with the system cannot be deleted

Data/APP

The directory installed by the user program. You have the permission to delete the directory.

Copy the APK file to this directory during installation

Data/Data

Store application data

Data/Dalvik-Cache

Install the DEX file in the apk to the Dalvik-cache directory (the DEX file is an executable file of the Dalvik virtual machine, and its size is about 1/4 of the size of the original APK file)

Installation Process: copy the APK installation package to the data/APP directory, decompress and scan the installation package, and save the DEX file (Dalvik bytecode) to the Dalvik-cache directory, and create the corresponding application data directory under the Data/data directory.

Uninstall process: delete the files and directories created under the preceding three directories during installation.

1. system application installation:
Packagemanagerservice handles the installation, uninstallation, and management of various applications. It is started by systemserver upon startup.

(Source file path: Android \ frameworks \ base \ Services \ Java \ com \ Android \ Server \ packagemanagerservice. Java)

Packagemanagerservice START process:

1. First scan and install the jar package under the "System \ framework" Directory

1. scandirli (mframeworkdir, packageparser. parse_is_system,

Scanmode | scan_no_dex );

2. Step 2 scan and install various system applications under the "System \ app" Directory

Scandirli (msystemappdir, packageparser. parse_is_system, scanmode );

3. Step 3 scan the "data \ app" directory, that is, third-party applications installed by the user

Scandirli (mappinstalldir, 0, scanmode );

4. Step 4 scan the "data \ app-private" directory, that is, installing the DRM-protected APK file (no such application has been encountered currently ).

Scandirli (mdrmappprivateinstalldir, 0, scanmode | scan_forward_locked );

Application Installation Process

1. scandirli (filedir, int flags, int scanmode) traverses the files installed in the specified directory

2. scanpackageli (filescanfile,

File destcodefile, filedestresourcefile, int parseflags,

Int scanmode) install the package file

3. scanpackageli (

File scanfile, file destcodefile, filedestresourcefile,

Packageparser. Package PKG, intparseflags, int scanmode)

Obtain the information structure of the installation package by parsing the installation package parsepackage

4. minstaller. Install (pkgname, PKG. applicationinfo. uid,

PKG. applicationinfo. UID); implements the installation process of File Replication

(Source file path: Frameworks \ base \ cmds \ installd. Install)

2. download the application from the market:
Google market applications can be used only after logon using a Gmail account. After selecting an application, download the installation package. During this process, a progress bar is displayed in the signal area of the mobile phone. After the download is complete, the packagemanager interface is automatically called for installation. The called interface is as follows:

Public voidinstallpackage (final URI packageuri, final ipackageinstallobserver observer, final int flags)

Final URI packageuri: The Path saved after the file is downloaded.

Final ipackageinstallobserver observer: process the returned installation result

Final int flags: the installation parameter for the Application downloaded from the market. The installation parameter is-R (replace)

Installation Process of installpackage interface functions:

1. Public voidinstallpackage (

Final URI packageuri, final ipackageinstallobserverobserver, final int flags,

Final string installerpackagename)

Final stringinstallerpackagename: After the installation is complete, this name is stored in settings. Generally, it is null, not a key parameter.

2. filetmppackagefile = copytempinstallfile (packageuri, Res );

Copy the APK file to a temporary file in the temporary directory

3. Private voidinstallpackageli (URI ppackageuri,

Int pflags, Boolean newinstall, string installerpackagename,

File tmppackagefile, packageinstalledinfo res)

Parse the temporary file and obtain the application package name pkgname = packageparser. parsepackagename (

Tmppackagefile. getabsolutepath (), 0 );

4. Determine if the install_replace_existing parameter is included, call replacepackageli (pkgname,

Tmppackagefile,

Destfilepath, destpackagefile, destresourcefile,

PKG, forwardlocked, newinstall, installerpackagename,

Res)

5. If not, call installnewpackageli (pkgname,

Tmppackagefile,

Destfilepath, destpackagefile, destresourcefile,

PKG, forwardlocked, newinstall, installerpackagename,

Res );

6. privatepackageparser. Package scanpackageli (

File scanfile, file destcodefile, filedestresourcefile,

Packageparser. Package PKG, intparseflags, int scanmode)

The process after scanpackageli is the same as that after the application is started.

3. Install from ADB
Android debug bridge (ADB) is a tool that comes with the SDK to manage devices. You can use the ADB command line to install applications on mobile phones or simulators. The source file of the function is pm. java.

(Source file path: Android \ frameworks \ base \ cmds \ PM \ SRC \ com \ Android \ commands \ PM \ pm. Java)

The ADB command line is in the form of ADB install <path_to_apk>. It can also contain installation parameters such as "-l" "-R" "-I" "-t"

Judge parameters in the runinstall () function

"-L"-install_forward_lock

"-R" -- install_replace_existing

"-I" -- installerpackagename

"-T" -- install_allow_test

Our commonly used parameter is-R, which indicates that the application with the same name already installed on the mobile phone is overwritten. Applications downloaded from the market are also directly input to install this parameter.

Runinstall and market call the same interface to install the application.

Public voidinstallpackage (android.net. Uri packageuri, Android. content. PM. ipackageinstallobserver observer, int flags, java. Lang. String installerpackagename)

4. Third-party application installation-install the SDK through the APK file in the SD card
The supervisor processes the installation and uninstallation interfaces in this mode, such:

Packageinstalleractivity parses the package and determines whether the APK file is available.

Create a temporary installation file/data/COM. Android. packageinstaller/files/apidemos.apk

Start the installation confirmation interface startinstallconfirm to list the basic information of the parsed application. If an application with the same name has been installed on your mobile phone, you need to confirm whether to replace and install the application.

After confirming the installation, start installappprogress and call the installation interface to complete the installation.

PM. installpackage (mpackageuri, observer, installflags );

Others:
1. The internal class appdirobserver of packagemanagerservice. Java implements the function of listening to the app Directory: when you drag an APK to the app directory, you can directly call scanpackageli to complete the installation.

2. The "Data/system/packages. xml" file in the mobile phone data area contains the basic information of all installed applications on the mobile phone, such as the installation path and applied permission.

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.