Android implements Silent Installation and uninstallation

Source: Internet
Author: User

Method 1: [call the interface method. Because some APIs for installing and detaching an application are hidden, you must download the Android system source code, after development and compilation in the source code, use the MM command to compile and generate an APK file]

Import java. Io. file;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. content. intent;
Import Android. content. PM. packageinfo;
Import Android. content. PM. packagemanager;
Import Android. content. PM. packagemanager. namenotfoundexception;
Import Android. content. PM. ipackageinstallobserver;
Import Android. content. PM. ipackagedeleteobserver;
Import Android. content. res. Resources;
Import Android. Graphics. drawable. drawable;
Import android.net. Uri;
Import Android. OS. environment;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. progressbar;
Import Android. widget. textview;
Import Android. widget. Toast;

Public class installactivity extends activity {
/** Called when the activity is first created .*/
Private string apk_name = "QQHD-mini.apk ";
Private button install = NULL;
Private button uninstall = NULL;
Private Final int install_complete = 1;
Private Final int uninstall_complete = 2;
Final Static int succeeded = 1;
Final Static int failed = 0;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. install_uninstall );
Install = (button) findviewbyid (R. Id. Install );
Uninstall = (button) findviewbyid (R. Id. Uninstall );
Uninstall. setonclicklistener (listener1 );
Install. setonclicklistener (listener2 );
}
Private onclicklistener listener2 = new onclicklistener (){
Public void onclick (view v ){
String filename = environment. getexternalstoragedirectory () + "/" + apk_name;

Uri uri = URI. fromfile (new file (filename ));

Int installflags = 0;
Packagemanager PM = getpackagemanager ();
Try {
Packageinfo Pi = PM. getpackageinfo ("com. Tencent. minihd. QQ ",
Packagemanager. get_uninstalled_packages );
If (Pi! = NULL ){
Installflags | = packagemanager. install_replace_existing;
}
} Catch (namenotfoundexception e ){}

Packageinstallobserver observer = new packageinstallobserver ();
PM. installpackage (Uri, observer, installflags, "com. Tencent. minihd. QQ ");
}
};

Class packageinstallobserver extends ipackageinstallobserver. Stub {
Public void packageinstalled (string packagename, int returncode ){
Message MSG = mhandler. obtainmessage (install_complete );
MSG. arg1 = returncode;
Mhandler. sendmessage (MSG );
}
};

Private handler mhandler = new handler (){
Public void handlemessage (Message MSG ){
Switch (msg. What ){
Case install_complete:
Int centertextlabel;
Drawable centertextdrawable = NULL;
If (msg. arg1 = succeeded ){
Toast. maketext (getapplicationcontext (), "Install APK succeeded", Toast. length_short). Show ();
} Else {}
If (centertextdrawable! = NULL ){}

Break;
Case uninstall_complete:
Int centertextlabel2;
Drawable centertextdrawable2 = NULL;
If (msg. arg1 = succeeded ){
Toast. maketext (getapplicationcontext (), "Uninstall APK succeeded", Toast. length_short). Show ();
} Else {}
If (centertextdrawable2! = NULL ){}

Break;
Default:
Break;
}
}
};
 
Private onclicklistener listener1 = new onclicklistener (){
Public void onclick (view v ){
Packagemanager PM = getpackagemanager ();
Packagedeleteobserver observer = new packagedeleteobserver ();
PM. deletepackage ("com. Tencent. minihd. QQ", observer, 0 );
}
};
 
Private class packagedeleteobserver extends ipackagedeleteobserver. Stub {
Public void packagedeleted (string packagename, int returncode ){
Message MSG = mhandler. obtainmessage (uninstall_complete );
MSG. arg1 = returncode;
Mhandler. sendmessage (MSG );
}
}

}

Install and uninstall related permissions <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"/>

 

Method 2: [obtain the root permission or have the same signature as Rom, that is, run the APK in the system as a system]

Private void test (){
Execcommand ("system/bin/PM install-R" + "sdcard/xxx.apk"); // place the xxx.apk in the built-in root directory (mnt/sdcard/xxx.apk)

// Execcommand ("system/bin/PM Uninstall" + package_name); // package_namexxx.apk package name
}

Public Boolean Execcommand (string cmd ){
Process = NULL;
Try {
Process = runtime.getruntime(cmd.exe C (CMD );
Process. waitfor ();
} Catch (exception e ){
Return false;
} Finally {
Try {
Process. Destroy ();
} Catch (exception e ){
}
}
Return true;
}

 

Other methods: some third-party jar packages

Method 1: [call the interface method. Because some APIs for installing and detaching an application are hidden, you must download the Android system source code, after development and compilation in the source code, use the MM command to compile and generate an APK file]

Import java. Io. file;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. content. intent;
Import Android. content. PM. packageinfo;
Import Android. content. PM. packagemanager;
Import Android. content. PM. packagemanager. namenotfoundexception;
Import Android. content. PM. ipackageinstallobserver;
Import Android. content. PM. ipackagedeleteobserver;
Import Android. content. res. Resources;
Import Android. Graphics. drawable. drawable;
Import android.net. Uri;
Import Android. OS. environment;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. progressbar;
Import Android. widget. textview;
Import Android. widget. Toast;

Public class installactivity extends activity {
/** Called when the activity is first created .*/
Private string apk_name = "QQHD-mini.apk ";
Private button install = NULL;
Private button uninstall = NULL;
Private Final int install_complete = 1;
Private Final int uninstall_complete = 2;
Final Static int succeeded = 1;
Final Static int failed = 0;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. install_uninstall );
Install = (button) findviewbyid (R. Id. Install );
Uninstall = (button) findviewbyid (R. Id. Uninstall );
Uninstall. setonclicklistener (listener1 );
Install. setonclicklistener (listener2 );
}
Private onclicklistener listener2 = new onclicklistener (){
Public void onclick (view v ){
String filename = environment. getexternalstoragedirectory () + "/" + apk_name;

Uri uri = URI. fromfile (new file (filename ));

Int installflags = 0;
Packagemanager PM = getpackagemanager ();
Try {
Packageinfo Pi = PM. getpackageinfo ("com. Tencent. minihd. QQ ",
Packagemanager. get_uninstalled_packages );
If (Pi! = NULL ){
Installflags | = packagemanager. install_replace_existing;
}
} Catch (namenotfoundexception e ){}

Packageinstallobserver observer = new packageinstallobserver ();
PM. installpackage (Uri, observer, installflags, "com. Tencent. minihd. QQ ");
}
};

Class packageinstallobserver extends ipackageinstallobserver. Stub {
Public void packageinstalled (string packagename, int returncode ){
Message MSG = mhandler. obtainmessage (install_complete );
MSG. arg1 = returncode;
Mhandler. sendmessage (MSG );
}
};

Private handler mhandler = new handler (){
Public void handlemessage (Message MSG ){
Switch (msg. What ){
Case install_complete:
Int centertextlabel;
Drawable centertextdrawable = NULL;
If (msg. arg1 = succeeded ){
Toast. maketext (getapplicationcontext (), "Install APK succeeded", Toast. length_short). Show ();
} Else {}
If (centertextdrawable! = NULL ){}

Break;
Case uninstall_complete:
Int centertextlabel2;
Drawable centertextdrawable2 = NULL;
If (msg. arg1 = succeeded ){
Toast. maketext (getapplicationcontext (), "Uninstall APK succeeded", Toast. length_short). Show ();
} Else {}
If (centertextdrawable2! = NULL ){}

Break;
Default:
Break;
}
}
};
 
Private onclicklistener listener1 = new onclicklistener (){
Public void onclick (view v ){
Packagemanager PM = getpackagemanager ();
Packagedeleteobserver observer = new packagedeleteobserver ();
PM. deletepackage ("com. Tencent. minihd. QQ", observer, 0 );
}
};
 
Private class packagedeleteobserver extends ipackagedeleteobserver. Stub {
Public void packagedeleted (string packagename, int returncode ){
Message MSG = mhandler. obtainmessage (uninstall_complete );
MSG. arg1 = returncode;
Mhandler. sendmessage (MSG );
}
}

}

Install and uninstall related permissions <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"/>

 

Method 2: [obtain the root permission or have the same signature as Rom, that is, run the APK in the system as a system]

Private void test (){
Execcommand ("system/bin/PM install-R" + "sdcard/xxx.apk"); // place the xxx.apk in the built-in root directory (mnt/sdcard/xxx.apk)

// Execcommand ("system/bin/PM Uninstall" + package_name); // package_namexxx.apk package name
}

Public Boolean Execcommand (string cmd ){
Process = NULL;
Try {
Process = runtime.getruntime(cmd.exe C (CMD );
Process. waitfor ();
} Catch (exception e ){
Return false;
} Finally {
Try {
Process. Destroy ();
} Catch (exception e ){
}
}
Return true;
}

 

Other methods: some third-party jar packages

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.