Batch install and uninstall

Source: Internet
Author: User

Have you ever tried to install and uninstall your applications in batches on your Android mobile phone? Now, it's easy to teach you how to do it. There are also background installation and uninstall problems:

1. Steps for background installation and uninstallation:

First, you need to use its Pm command, such


Pm install [-l] [-r] [-t] [-I INSTALLER_PACKAGE_NAME] [-s] [-f] PATH
Pm uninstall [-k] PACKAGE
The code implementation is also very simple. You can use the stream method to perform related operations:

Public static void execCommand (String... command ){
02 Process process = null;
03 try {
04 process = new ProcessBuilder (). command (command). start ();
05 // we can read the command execution result through the stream.
06 // InputStream in = process. getInputStream ();
07 // OutputStream out = process. getOutputStream ();
08 // InputStream err = process. getErrorStream ();
09} catch (IOException e ){
10 e. printStackTrace ();
11} finally {
12 if (process! = Null)
13 process. destroy ();
14}
15}

1 execCommand ("pm", "install", "-f", filePath); // install apk. filePath is the path of the apk file, for example,/mnt/sdcard/ApiDemos.apk.
2 execCommand ("pm", "uninstall", packageName); // uninstall the apk. packageName is the package name, for example, com. example. android. apis.
Note:

When compiling an apk, add android: sharedUserId = "android. uid. system ". After compilation is complete, the Installation cannot be properly performed. The Installation error: INSTALL_FAILED_SHARED_USER_INCOMPATIBLE error may occur. At this time, re-sign the apk.
 
Find platform in android source code \ build \ target \ product \ security. pk8 and platform. x509.pem files. Find signapk in the out directory of the android editor. jar package (source directory \ build \ tools \ signapk), and compiled apk(such as pmdemo.apk) in the same directory, before re-signing, open the apk file with rarfile, enter the META-INF directory, CERT. SF and CERT. delete the two RSA files and execute the following command in the command line:

1 java-jar signapk. jar platform. x509.pem platform. pk8 PMDemo.apk NewPMDemo.apk
Uninstall the old apk before installation, so that the apk after re-signing can be installed normally.

It's easy. Let's take a look at the batch operation below:
Check the Code directly:


Generally, android does not provide a silent upper-layer interface. We need to call this hidden interface in the android source code to complete the Silent Installation.
The most important thing is to refer to packages/apps/PackageInstaller In the android system directory,
There are two files PackageInstallerActivity. java, InstallAppProgress. java. The former is the installation application that we usually see with the prompt dialog box, and the latter is the intent called after the installation.
A key class for silent installation is provided. This class is successfully compiled under android2.2, and batch installation can be achieved through the instatllBatch loop call interface.
Of course, do not forget to add permissions to the last application.
 
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. INSTALL_PACKAGES"/>
<Uses-permission android: name = "android. permission. DELETE_PACKAGES"/>
<Uses-permission android: name = "android. permission. CLEAR_APP_CACHE"/>
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>
<Uses-permission android: name = "android. permission. CLEAR_APP_USER_DATA"/>
 
Package com. android. util;
 
Import java. io. File;
 
Import java. io. FileNotFoundException;
 
Import java. io. FileOutputStream;
 
Import java. io. IOException;
 
Import android. content. Context;
 
Import android. content. Intent;
 
Import android. content. pm. PackageInfo;
 
Import android. content. pm. PackageManager;
 
Import android. content. pm. PackageManager. NameNotFoundException;
 
Import android. content. pm. ApplicationInfo;
 
Import android. content. pm. PackageParser;
 
Import android.net. Uri;
 
Import android. util. Log;
 
Import android. util. DisplayMetrics;
 
Import android. content. pm. IPackageInstallObserver;
 
Import android. content. pm. IPackageDeleteObserver;
 
Import android. OS. FileUtils;
 
Import android. OS. Handler;
 
Import android. OS. Message;
 

 
Public class PackageInstaller {
 

 
Private File mTmpFile;
 
Private final int INSTALL_COMPLETE = 1;
 
Final static int SUCCEEDED = 1;
 
Final static int FAILED = 0;
 
Private final static String TAG = "PackInstaller ";
 
Private Context mContext;
 
Private ApplicationInfo mAppInfo;
 
Public PackageInstaller (Context context ){
 
MContext = context;
 
}
 
Public void install (String path, String packageName ){
 
Intent intent = new Intent (Intent. ACTION_VIEW );
 
Intent. setDataAndType (Uri. fromFile (new File (path )),
 
"Application/vnd. android. package-archive ");
 
MContext. startActivity (intent );
 
}
 

 
Public void instatllBatch (String path ){
 
Log. I (TAG, "path =" + path );
 
Int installFlags = 0;
 
Uri mPackageURI = Uri. fromFile (new File (path ));
 
PackageParser. Package mPkgInfo = getPackageInfo (mPackageURI );
 
MAppInfo = mPkgInfo. applicationInfo;
 
String packageName = mAppInfo. packageName;
 
Log. I (TAG, "=== install packageName =" + packageName );
 
PackageManager pm = mContext. getPackageManager ();
 
Try {
 
PackageInfo pi = pm. getPackageInfo (packageName,
 
PackageManager. GET_UNINSTALLED_PACKAGES );
 
If (pi! = Null ){
 
InstallFlags | = PackageManager. INSTALL_REPLACE_EXISTING;
 
}
 
} Catch (NameNotFoundException e ){
 
}
 
If (installFlags & PackageManager. INSTALL_REPLACE_EXISTING )! = 0 ){
 
Log. w (TAG, "Replacing package:" + packageName );
 
}
 

 
PackageInstallObserver observer = new PackageInstallObserver ();
 
Pm. installPackage (mPackageURI, observer, installFlags,
 
PackageName );
 
}
 
Private class PackageInstallObserver extends IPackageInstallObserver. Stub {
 
Public void packageInstalled (String packageName, int returnCode ){
 
// Message msg = mHandler. obtainMessage (INSTALL_COMPLETE );
 
// Msg. arg1 = returnCode;
 
// MHandler. sendMessage (msg );
 
Log. I (TAG, "=== INSTALL_COMPLETE ");
 
}
 
}
 
Private class PackageDeleteObserver extends IPackageDeleteObserver. Stub {
 
Public void packageDeleted (boolean succeeded ){
 
// Message msg = mHandler. obtainMessage (UNINSTALL_COMPLETE );
 
// Msg. arg1 = succeeded? SUCCEEDED: FAILED;
 
// MHandler. sendMessage (msg );
 
Log. I (TAG, "=== UNINSTALL_COMPLETE ");
 
}
 
}
 
Public void uninstall (String packageName ){
 
Uri packageURI = Uri. parse ("package:" + packageName );
 
Intent uninstallIntent = new Intent (Intent. ACTION_DELETE,
 
PackageURI );
 
MContext. startActivity (uninstallIntent );
 
}
 

 
Public void uninstallBatch (String packageName ){
 
PackageDeleteObserver observer = new PackageDeleteObserver ();
 
MContext. getPackageManager (). deletePackage (packageName, observer, 0 );
 

 
}
 
Public PackageParser. Package getPackageInfo (Uri packageURI ){
 
Final String archiveFilePath = packageURI. getPath ();
 
PackageParser packageParser = new PackageParser (archiveFilePath );
 
File sourceFile = new File (archiveFilePath );
 
DisplayMetrics metrics = new DisplayMetrics ();
 
Metrics. setToDefaults ();
 
PackageParser. Package pkg = packageParser. parsePackage (sourceFile,
 
ArchiveFilePath, metrics, 0 );
 
// Nuke the parser reference.
 
PackageParser = null;
 
Return pkg;
 
}
 
Public ApplicationInfo getApplicationInfo (Uri packageURI ){
 
Final String archiveFilePath = packageURI. getPath ();
 
PackageParser packageParser = new PackageParser (archiveFilePath );
 
File sourceFile = new File (archiveFilePath );
 
DisplayMetrics metrics = new DisplayMetrics ();
 
Metrics. setToDefaults ();
 
PackageParser. Package pkg = packageParser. parsePackage (sourceFile, archiveFilePath, metrics, 0 );
 
If (pkg = null ){
 
Return null;
 
}
 
Return pkg. applicationInfo;
 
}
 
Private Handler mHandler = new Handler (){
 
Public void handleMessage (Message msg ){
 
Switch (msg. what ){
 
Case INSTALL_COMPLETE:
 
If (msg. arg1 = SUCCEEDED ){
 

 
} Else {}
 
Break;
 
Default:
 
Break;
 
}
 
}
 
};
 
}

 

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.