Android + Eclipse + background silent installation (at a glance)

Source: Internet
Author: User

First of all, there are three classes.

[Java]View Plaincopy
    1. Import Android.content.pm.IPackageInstallObserver;
    2. Import android.content.pm.ipackageinstallobserver$stub;
    3. Import Android.content.pm.PackageManager;

Among them, Ipackageinstallobserver and ipackageinstallobserver$stub These two classes, in Android SDK 1.5 will have, after the version seems to be gone. There are two methods of getting

First, you can extract from Android 1.5 Android. Jar (the jar is actually a zip file, so you will be able to open it using WinRAR, or change its suffix to a zip file, open it directly, when it's finished, and change it back). ANDROIDSKD /platforms/android**/android.jar. (* * refers to each version number, for example: 4.2 is ANDROID-17). In this Android SDK 1.5, is the Android-3/android.jar compression package.

Second, (I chose this method) by compressing files from the/system/framework/framework.jar in the Android 1.5 virtual machine. Command: ADB pull/system/framework/framework.jar '/home/china/desktop/framework.jar ', through Dex2jar conversion to get a jar package, is the jar inside there, Ipackageinstallobserver and Ipackageinstallobserver$stub and Packagemanager these three documents. (The reason, also need to decompress packagemanage later will say)

The candidate is Android 4.2 compiled, so put these three files in the. Android Sdk/android-17/android.jar file. Put it in the appropriate directory, that is,/android/comtent/pm/.
(It's best to back up Android.jar).

Create a new project with Eclipse.

[Java]View Plaincopy
  1. import Android.content.pm.IPackageInstallObserver; //Must be imported
  2. nbsp public void Install (String filename,string packagename)//
  3. {
  4. Uri uri = uri.fromfile (new File);
  5. int installflags = 0;
  6. Packagemanager pm = Getpackagemanager ();
  7. try {
  8. PackageInfo pi = pm.getpackageinfo (packagename,packagemanager.get_uninstalled_packages); //  
  9. if (pi! = null) {
  10. Installflags |= packagemanager.install_replace_existing;
  11. }
  12. } catch (Namenotfoundexception e) {
  13. }
  14. Toast.maketext (mainactivity.   This, "install start", Toast.length_short). Show ();
  15. Packageinstallobserver observer = new Packageinstallobserver ();
  16. Pm.installpackage (URI, Observer, Installflags, PackageName); //Below are detailed
  17. }
  18. //For displaying results
  19. class Packageinstallobserver extends Ipackageinstallobserver.stub {
  20. public void packageinstalled (String packagename, int returncode) {//if ReturnCode = = 1 is successful
  21. }
  22. };
  23. @Override
  24. protected void OnCreate (Bundle savedinstancestate) {
  25. super.oncreate (savedinstancestate);
  26. Setcontentview (R.layout.activity_main);
  27. String fileName = environment.getexternalstoragedirectory () + "/uc.apk"; //Of course, you must put the uc.apk in this folder before
  28. Install (FileName,"Com.uc.browser"); Called
  29. }


is packagemanager.installpackage in 4.2 of the Packagemanager bag, there is no such function, because, it is a abstrackt virtual class, so as long as it can be compiled successfully, it is possible, but on Android 1.5 in the Android.jar, packagemanager.installpackage in the function parameter is 3, let me very not annoying, but through the second method can be.

The runtime must include the following permissions in Androidmanifest.xml

[HTML]View Plaincopy
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="Com.hyy.pminstall"
  3. android:versioncode="1"
  4. android:versionname="1.0"
  5. android:shareduserid="Android.uid.system"
  6. >
  7. <uses-permission android:name="Android.permission.INSTALL_PACKAGES"/>
  8. <uses-permission android:name="Android.permission.DELETE_PACKAGES"/>
  9. <uses-permission android:name="Android.permission.CLEAR_APP_USER_DATA"/>
  10. <uses-permission android:name="Android.permission.CLEAR_APP_CACHE"/>
  11. <uses-permission android:name="Android.permission.INTERNET" />

It will appear when you join: Permission is just granted to system apps, click Project->clear.

Compiled, the APK will run in error, because this program will use the high privilege, so you must use the System key to check this APK package.

First, from the APK source in the. \build\target\product\security\ folder, Platform.x509.pem platform.pk8 two files.

Then, use the Java-jar signapk.jar platform.x509.pem platform.pk8 modsystime.apk modsystime_new.apk command to sign the package.

Finally, the ADB install new.apk.

This has to be modified by itself, become a universal, installer.

I will put all of these files: my resources: http://download.csdn.net/detail/he702477275/5296315

_____________________________________________

installation, uninstallation and update of Android programsCategory:------Android Project 2013-06-15 16:44 269 people read reviews (0) favorite reports

Xu Jianxiang ([email protected])
Date: 2010/07/28
Website: http://www.anymobile.org

Installer: Software from scratch.

Uninstall program: Software from there to none.

Update: Software overlay installation, you can keep the original version of the data, improve the software version.

How to install the program:

1, through the intent mechanism, call up the system installation application, reinstall the application, will retain the original application data.

String fileName = environment.getexternalstoragedirectory () + apkname;

Uri uri = uri.fromfile (new File);

Intent Intent = new Intent (Intent. Action_view);

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

StartActivity (Intent);

2, directly call the installation interface.

Uri Mpackageuri = URI. FromFile (new File (environment. getExternalStorageDirectory() + apkname));

int installflags = 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 = new packageinstallobserver ();

Pm.installpackage (Mpackageuri, Observer, installflags);

Install app permissions:Android.permission.INSTALL_PACKAGES

System applications (installed under/system/app) can be used in this way, and third-party apps cannot request installation uninstall permissions.

Java.lang.SecurityException:Neither User 10039 Nor current process has android.permission.INSTALL_PACKAGES.

3, execute the install command.

Install–r update installation, default new installation, if you do not attach the-R parameter, you will be aware of the original application of the data, the version of the same cannot be installed.

(1) am start ...

(2) runtime.exec (string[] args)

(3) class<?> Execclass = Class.forName ("android.os.Exec");

4, execute cp/adb push command.

The system detects that the application has an update and automatically completes the reinstallation.

5, through the third-party software implementation.

Market,eoe,etrackdog are implemented with the first method of updating.

Advantage: The system core application controls the installation program;

Disadvantage: Unable to control the installation process, after the installation is complete, you can not start the application immediately, requires user confirmation;

Example: 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, through the intent mechanism, bring up the system uninstall application.

Uri Packageuri = Uri.parse ("Package:your.app.id");

Intent Intent = new Intent (Intent. Action_delete);

StartActivity (Intent);

2. Call the Unload interface directly.

Packageinstallobserver observer = new packageinstallobserver ();

Pm.installpackage (Mpackageuri, Observer, installflags);

Uninstall app permissions:Android.permission.DELETE_PACKAGES

3. Run RM apk installation file and call uninstall app after system detection.

Remark Description:

Android application installation, in the system setup there is an item, whether to install the unknown source, in the software update, the need to detect this option, if the hook, only allow the installation of the installation of the market source, if there is no hook, the system installs the app will prompt the user settings, if you choose to set , you cannot return to the installation interface after Setup, and if you choose to cancel, the installer is available. So, if it is an update, be sure to check the license installation source settings before downloading, or detect if a new installer has been downloaded before downloading, and avoid repeatedly downloading the installer.

The relevant code is as follows:

1. int result = Settings.Secure.getInt (Getcontentresolver (), Settings.Secure.INSTALL_NON_MARKET_APPS, 0);

2. if (result = = 0) {

3.//Show some dialog here

4.//...

5.//May be Show application Settings dialog manually

6. Intent Intent = new Intent ();

7. Intent.setaction (settings.action_application_settings);

8. StartActivity (Intent);

9.}

public static final class Settings.secure extends settings.namevaluetable

public static final String Install_non_market_apps

SINCE:API Level 3

Whether the installer should allow installation of apps downloaded from sources other than the Android market (ven Ding machine). 1 = Allow installing from and sources 0 = only allow installing from the Android market.

Here are a few steps to the program update Demo:

over!

    • The previous Java filter string method implementation
    • Next Android + Eclipse + background silent installation (a look will be)

Android + Eclipse + background silent installation (at a glance)

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.