1. Send messages through the Intent message mechanism and call the system application to install/uninstall the apk.
(1) Call the system installation application to enable the system to automatically install the apk
String fileName = "/data/com. zlc. ipanel. operate/FileOperate.apk ";
Uri uri = Uri. fromFile (new File (fileName ));
Intent intent = new Intent (Intent. ACTION_VIEW );
Intent. setDataAndType (uri, "application/vnd. android. package-archive ");
StartActivity (intent );
Appeal installation can not only install a new apk (from scratch), but also be used to update the old apk (version update). During version update, the two apk signatures must be consistent.
If it is a common application installed under the data/partition, the new apk will replace the old apk.
If it is a system application, it is generally installed under system/app. After the update, the old apk under system/app/still exists, and the system will copy the new apk to data/app. Although there are two old and new apks at the same time, the system selects the new apks for data Partition to run during the runtime. If the new apk is uninstalled, the apk for data Partition is deleted, the old apk in the system directory is used to start the program again.
(2) Start the uninstall application of the system and enable the system to automatically uninstall the apk.
// Create a URI using the package name of the program
Uri packageURI = Uri. parse ("package: com. zlc. ipanel ");
// Create Intent
Intent intent = new Intent (Intent. ACTION_DELETE, packageURI );
// Execute the uninstall program
StartActivity (intent );
Different from apk installation, the Intent message is changed here: ACTION_DELETE. The apk installation uses (ACTION_VIEW)
2. Uninstall, install, and obtain permissions for the apk by calling the interface packageManager provided by the system (Silent Installation)
(1) platform signature is required (write an Android. mk file to compile the file in the source code is the most convenient. Of course, there are many other methods on the Internet, such as singapk command line Signature)
(2) because the API is not publicly available, you need to introduce the source code (just make a vest and do not participate in compilation)
(3) Add the apk installation permission in AndroidManifest. xml.
<Uses-permission android: name = "android. permission. INSTALL_PACKAGES"/>
(4) If the upgrade fails and the Unable to open zip '/data/FileOperate.apk': The Permission c method can be modified before the upgrade.
PackageManager pm = this. getPackageManager ();
File file = new File ("/data/com. zlc. ipanel. operate/FileOperate.apk ");
Pm. installPackage (Uri. fromFile (file), null,
PackageManager. INSTALL_REPLACE_EXISTING, "com. zlc. ipanel ");
(5) The following example uses Eclipse to implement Silent Installation/uninstall.
1) follow the Silent Installation requirements described above to implement our operations step by step. First, prepare the vest (the interface to be called for Silent Installation)
Because some of these interfaces are implemented through aidl, we can modify the required vest.
PackageManager. java
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License ");
* You may not use this file before t in compliance with the License.
* You may obtain a copy of the License
*
* Http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* Distributed under the License is distributed on an "as is" BASIS,
* Without warranties or conditions of any kind, either express or implied.
* See the License for the specific language governing permissions and
* Limitations under the License.
*/
Package android. content. pm;
Import android. content. Context;
Import android.net. Uri;
/**
* Class for retrieving various kinds of information related to the application
* Packages that are currently installed on the device.
*
* You can find this class through {@ link Context # getPackageManager }.
*/
Public abstract class PackageManager {
/**
* Flag parameter for {@ link # installPackage} to indicate that you want to replace an already
* Installed package, if one exists.
* @ Hide
*/
Public static final int INSTALL_REPLACE_EXISTING = 0x00000002;
/**
* @ Hide
*
* Install a package. Since this may take a little while, the result will
* Be posted back to the given observer. An installation will fail if the calling context
* Lacks the {@ link android. Manifest. permission # INSTALL_PACKAGES} permission, if
* Package named in the package file's manifest is already installed, or if there's no space
* Available on the device.
*
* @ Param packageURI The location of the package file to install. This can be a 'file: 'or
* 'Content: 'uri.
* @ Param observer An observer callback to get notified when the package installation is
* Complete. {@ link IPackageInstallObserver # packageInstalled (String, int)} will be
* Called when that happens. observer may be null to indicate that no callback is desired.
* @ Param flags-possible values: {@ link # INSTALL_FORWARD_LOCK },
* {@ Link # INSTALL_REPLACE_EXISTING}, {@ link # INSTALL_ALLOW_TEST }.
* @ Param installerPackageName Optional package name of the application that is creating
* Installation. This identifies which market the package came from.
*/
Public abstract void installPackage (
Uri packageURI, IPackageInstallObserver observer, int flags,
String installerPackageName );
/**
* Attempts to delete a package. Since this may take a little while, the result will
* Be posted back to the given observer. A deletion will fail if the calling context
* Lacks the {@ link android. Manifest. permission # DELETE_PACKAGES} permission, if
* Named package cannot be found, or if the named package is a "system package ".
* (TODO: include pointer to documentation on "system packages ")
*
* @ Param packageName The name of the package to delete
* @ Param observer An observer callback to get notified when the package deletion is
* Complete. {@ link android. content. pm. IPackageDeleteObserver # packageDeleted (boolean)} will be
* Called when that happens. observer may be null to indicate that no callback is desired.
* @ Param flags-possible values: {@ link # DONT_DELETE_DATA}
*
* @ Hide
*/
Public abstract void deletePackage (
String packageName, IPackageDeleteObserver observer, int flags );
}
Callback interface IPackageInstallObserver. java (modified vest)
Package android. content. pm;
Import android. OS. RemoteException;
Public interface IPackageInstallObserver {
Public class Stub implements IPackageInstallObserver {
Public void packageInstalled (String packageName, int returnCode)
Throws RemoteException {
// TODO Auto-generated method stub
}
}
}
Successfully uninstalled callback interface IPackageDeleteObserver. java (modified vest)
Package android. content. pm;
Public interface IPackageDeleteObserver {
Public class Stub implements IPackageDeleteObserver {
Public void packageDeleted (String packageName, int returnCode ){
// TODO Auto-generated method stub
}
}
}
2) After the vest is ready, we will start to implement our operation logic.
Implement the Silent Installation/uninstall method and register the callback (the corresponding value will be returned no matter the failure is successful)
// Silent Installation
Public static void installApkDefaul (Context context, String fileName, String pakcageName ){
Log. d (TAG, "jing mo an zhuang ");
File file = new File (fileName );
Int installFlags = 0;
If (! File. exists ())
Return;
Log. d (TAG, "jing mo an zhuang out ");
InstallFlags | = PackageManager. INSTALL_REPLACE_EXISTING;
PackageManager pm = context. getPackageManager ();
IPackageInstallObserver observer = new MyPakcageInstallObserver ();
Pm. installPackage (Uri. fromFile (file), observer, installFlags, pakcageName );
}
// Silently uninstall
Public static void uninstallApkDefaul (Context context, String packageName ){
PackageManager pm = context. getPackageManager ();
IPackageDeleteObserver observer = new MyPackageDeleteObserver ();
Pm. deletePackage (packageName, observer, 0 );
}
// Silent uninstall callback
Private static class MyPackageDeleteObserver extends IPackageDeleteObserver. Stub {
@ Override
Public void packageDeleted (String packageName, int returnCode ){
// TODO Auto-generated method stub
Log. d (TAG, "returnCode =" + returnCode); // if 1 is returned, the uninstall is successful.
}
}
// Silent Installation callback
Private static class MyPakcageInstallObserver extends IPackageInstallObserver. Stub {
@ Override
Public void packageInstalled (String packageName, int returnCode)
Throws RemoteException {
// TODO Auto-generated method stub
Log. I (TAG, "returnCode =" + returnCode); // if 1 is returned, the installation is successful.
}
}
3) after the main logic is implemented, add the installation and uninstallation permissions in AndroidManifest. xml.
<Uses-permission android: name = "android. permission. INSTALL_PACKAGES"/>
<Uses-permission android: name = "android. permission. DELETE_PACKAGES"/>
4) Run Eclipse to generate the apk of the application. The system signature is required below. Write a script to replace it with the system signature.
To use the signature package signature, we first need to retrieve the following platform. x509.pem, platform. pk8, and signapk. jar files from the source code.
Script 1.bat (put the generated apk and the above three files under the same directory, usually the bin directory under the Project)
Java-jar signapk. jar platform. x509.pem platform. pk8 FileUpdateControl.apk 1.apk
Adb uninstall com. zlc. ipanel. operate
Adb install 1.apk
5) when the apk permission to be downloaded to the system is insufficient (a permission error is prompted for Silent Installation. It is possible that the current apk only has (-rw -------) Permission)
You can use the following three methods to modify permissions:
1. Use Runtime to start a thread to modify permissions
Try {
Runtime.getruntime(cmd.exe c ("chmod 777" + file. getCanonicalPath ());
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
2. Write an native Interface and directly call the c interface through jni to modify the permission.
3. When FileUtils is used, this class is hidden by default. The official sdk does not include this class. Therefore, to use this class for code, you need to compile the project in the android source code.
FileUtils. setPermissions (f. getAbsolutePath (), FileUtils. S_IRUSR | FileUtils. S_IWUSR | FileUtils. S_IRGRP | FileUtils. S_IROTH,-1,-1 );
3. Other apk installation methods
(1) run the adb command in the cmd window to install/uninstall apk.
(2) the system automatically installs the cp apk in the system/app directory.
(3) directly call the pm command through code to install and uninstall the apk.
1 |
ExecCommand ("pm", "install", "-f", filePath); // install apk. filePath is the path of the apk file, for example,/sdcard/operate.apk. |
2 |
ExecCommand ("pm", "uninstall", packageName); // uninstall the apk. packageName is the package name, such as com. zlc. ipanel. |