Install, uninstall, update upgrade for Android APK (silent installation via Eclipse)

Source: Internet
Author: User

First, through the intent message mechanism to send messages, call system application to implement APK installation/uninstallation.
(1) Call the system to install the application, let the system automatically install APK

String fileName = "/data/data/com.zlc.ipanel.operate/fileoperate.apk";
Uri uri = uri.fromfile (new File);
Intent Intent = new Intent (Intent.action_view);
Intent.setdataandtype (URI, "application/vnd.android.package-archive");
StartActivity (Intent);

Appeal installation not only can install a new apk (from scratch), but also can be used to update the old apk (version update), in the version update, you must ensure that the two APK signature is consistent.
If it is a generic application, install it under the data/partition and the new apk will replace the old apk.
If it is a system application, generally installed under System/app, after the update, the old apk still exists under system/app/, the system will copy the new apk to Data/app. Although there are two new and old apk, but the runtime system select the data partition of the new apk run, if the uninstall is to delete the data partition of the APK, restart the program is running the old apk in the system directory.
(2) Start the system uninstall application, let the system automatically uninstall APK

Create a URI from the program's package name
Uri Packageuri = Uri.parse ("Package:com.zlc.ipanel");
Create Intent Intent
Intent Intent = new Intent (Intent.action_delete,packageuri);
To perform an uninstall program
StartActivity (Intent);

Unlike the APK installation, the intent message is changed here: action_delete,apk installation is used (Action_view)

Second, by invoking the interface provided by the system Packagemanager apk to uninstall, install, get permissions, etc. (silent installation)
(1) Need platform signature (write a android.mk in the source code to compile the most convenient, of course, there are many other methods on the Internet, such as: singapk command line signature)
(2) because the call is not public API, so the need to introduce the source code (just a vest, do not participate in the compilation)
(3) Add install APK permissions in Androidmanifest.xml
<uses-permission android:name= "Android.permission.INSTALL_PACKAGES"/>
(4) If the upgrade fails to appear unable to open zip '/data/fileoperate.apk ': Permission denied, the description is insufficient permissions, it is possible that the current apk only (-rw-------) permissions, It is possible to make the upgrade by modifying the permissions through the Runtime.exec method.

Packagemanager pm = This.getpackagemanager ();
File File = new file ("/data/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/uninstallation.
1) Follow our silent installation requirements as described above to achieve our actions step by step. First, prepare the vest (the interface to be called for silent installation)
Due to the call of the system is not exposed interfaces, and some of these interfaces are implemented through AIDL, below we will need to modify the vest.
Packagemanager.java

/*
* Copyright (C) 2006 the Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You are not a use this file except in compliance with the License.
* Obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable or agreed to writing, software
* Distributed under the License is distributed on a "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 is currently installed on the device.
*
* can find this class through {@link Context#getpackagemanager}.
*/
Public abstract class Packagemanager {

/**
* Flag parameter for {@link #installPackage} to indicate this want to the replace an already
* Installed package, if one exists.
* @hide
*/
public static final int install_replace_existing = 0x00000002;

/**
* @hide
*
* Install a package. Since this could take a little while, the result would
* be posted back to the given observer. An installation would fail if the calling context
* Lacks the {@link Android. Manifest.permission#install_packages} permission, if the
* 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 is a ' file: ' or a
* ' content: ' URI.
* @param observer an observer callback to get notified when the package installation are
* Complete. {@link ipackageinstallobserver#packageinstalled (String, int)} 'll be
* Called when that happens. Observer may null to indicate this 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's performing the
* Installation. This identifies which, the came from.
*/
public abstract void InstallPackage (
Uri Packageuri, ipackageinstallobserver Observer, int flags,
String installerpackagename);

/**
* Attempts to delete a package. Since this could take a little while, the result would
* be posted back to the given observer. A deletion would fail if the calling context
* Lacks the {@link Android. Manifest.permission#delete_packages} permission, if the
* Named package cannot was found, or if the named package was 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 are
* Complete. {@link android.content.pm.ipackagedeleteobserver#packagedeleted (Boolean)} would be
* Called when that happens. Observer may null to indicate this no callback is desired.
* @param flags-possible values: {@link #DONT_DELETE_DATA}
*
* @hide
*/
public abstract void Deletepackage (
String PackageName, ipackagedeleteobserver Observer, int flags);

}

Installation of the successful 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

}
}
}

Uninstall successful callback interface Ipackagedeleteobserver.java (modified waistcoat)

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) Once the vest is ready, we begin to implement our operational logic.
Implement a silent Install/unload method, and register a callback (the corresponding value will be returned regardless of successful failure)

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);
}
Silent Uninstallation
public static void Uninstallapkdefaul (Context context,string packagename) {
Packagemanager pm = Context.getpackagemanager ();
Ipackagedeleteobserver observer = new Mypackagedeleteobserver ();
Pm.deletepackage (PACKAGENAME, observer, 0);
}
Silent Unload 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);//return 1 for Uninstall success
}

}
Silent Install 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);//return 1 for installation success
}
}

3) After the main logic is implemented, add the install and uninstall permissions in Androidmanifest.xml

<uses-permission android:name= "Android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name= "Android.permission.DELETE_PACKAGES"/>

4) Run Eclipse and build the APK for the app, and the system signature will be next. Write a script to implement the replacement system signature
Using Signature Package Signatures first we have to go to the source to get the following files Platform.x509.pem, Platform.pk8, Signapk.jar

Script 1.bat (and the generated apk and the above three files are placed under the same directory, typically the bin directory below 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 download to the system inside the APK permission is not enough (silent installation prompt permission issue, it is possible that the current apk only (-rw-------) permissions)
You can modify permissions in the following three ways

1. Use runtime to start a thread modification permission

try {
Runtime.getruntime (). EXEC ("chmod 777" + File.getcanonicalpath ());
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

2. Write a native interface to modify permissions directly through the interface of JNI call C.
3. Use Fileutils, this class is hidden by default, the official SDK does not include this class, so the code to use this class, you need to put the project into the Android source code to compile
Fileutils.setpermissions (F.getabsolutepath (), FILEUTILS.S_IRUSR | FILEUTILS.S_IWUSR | Fileutils.s_irgrp | Fileutils.s_iroth,-1,-1);

Third, the other APK installation method

(1) via CMD window adb command, Install/uninstall apk.
(2) CP apk to System/app directory, the system will be installed automatically
(3) Execute APK installation and uninstallation by calling PM command directly from code

1

ExecCommand ("PM", "Install", "-F", filePath);//install Apk,filepath as the apk file path, such as/sdcard/operate.apk

2

ExecCommand ("PM", "Uninstall", packagename);//uninstall Apk,packagename for package name, such as Com.zlc.ipanel

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.