/** * Android app apk install, replace and Uninstall * * @description: * @author ldm * @date 2016-4-27 PM 4:53:01 */public class Apkoperatoract
Ivity extends Activity {//install private static final int install_request = 1;
Unload private static final int uninstall_request = 2;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (r.layout.operator_apk_layout);
Initbuttonsandlisteners ();
}/** * Initialize button and Listen event * * @description: * @author LDM * @date 2016-4-27 4:29:12 * *
private void Initbuttonsandlisteners () {button button = (button) Findviewbyid (R.id.unknown_source);
Button.setonclicklistener (Installapklistener);
Button = (button) Findviewbyid (R.id.my_source);
Button.setonclicklistener (Installresultlistener);
Button = (button) Findviewbyid (r.id.replace);
Button.setonclicklistener (Replaceapklistener); BUtton = (Button) Findviewbyid (R.id.uninstall);
Button.setonclicklistener (Uninstallapklistener);
Button = (button) Findviewbyid (R.id.uninstall_result);
Button.setonclicklistener (Uninstallresultlistener); } @Override public void Onactivityresult (int requestcode, int resultcode, Intent Intent) {if (requestcod
E = = install_request) {if (ResultCode = = ACTIVITY.RESULT_OK) {showtoastmsg ("Installation succeeded!"); } else if (ResultCode = = activity.result_canceled) {showtoastmsg ("cancels the installation.
"); } else {showtoastmsg ("Installation failed.
");
}} else if (Requestcode = = uninstall_request) {if (ResultCode = = ACTIVITY.RESULT_OK) { Showtoastmsg ("Uninstall succeeded.
"); } else if (ResultCode = = activity.result_canceled) {showtoastmsg ("Cancel uninstall.
"); } else {showtoastmsg ("Unload failed.
"); }}} Private ONclicklistener Installapklistener = new Onclicklistener () {public void OnClick (View v) {//install APK
Intent Intent = new Intent (intent.action_install_package);
Intent.setdata (Uri.fromfile (Checkapkfile ("helloactivity.apk"));
StartActivity (Intent);
}
};
Private Onclicklistener Installresultlistener = new Onclicklistener () {public void OnClick (View v) {
Intent Intent = new Intent (intent.action_install_package);
Intent.setdata (Uri.fromfile (Checkapkfile ("helloactivity.apk"));
Unknown installation source Intent.putextra (Intent.extra_not_unknown_source, true);
Returns the result Intent.putextra (Intent.extra_return_result, true); The name of the installation package Intent.putextra (Intent.extra_installer_package_name, Getapplicationinfo (). Packag
ENAME);
Startactivityforresult (Intent, install_request);
}
}; Private OnclicklistenEr replaceapklistener = new Onclicklistener () {public void OnClick (View v) {Intent Intent = new Inte
NT (intent.action_install_package);
Intent.setdata (Uri.fromfile (Checkapkfile ("helloactivity.apk"));
Intent.putextra (Intent.extra_not_unknown_source, true);
Intent.putextra (Intent.extra_return_result, true);
Replace the installation package Intent.putextra (Intent.extra_allow_replace, true);
Intent.putextra (Intent.extra_installer_package_name, Getapplicationinfo (). PackageName);
Startactivityforresult (Intent, install_request);
}
}; Private Onclicklistener Uninstallapklistener = new Onclicklistener () {public void OnClick (View v) {/
/uninstall Intent Intent = new Intent (intent.action_uninstall_package);
Intent.setdata (Uri.parse ("package:com.ldm.study.SmsActivity"));
StartActivity (Intent);
}
}; Private OnClickListener Uninstallresultlistener = new Onclicklistener () {public void OnClick (View v) {Intent Intent
= new Intent (intent.action_uninstall_package);
Intent.setdata (Uri. Parse ("package:com.example.android.helloactivity"));
Set unload result Intent.putextra (Intent.extra_return_result, true);
Startactivityforresult (Intent, uninstall_request);
}
};
Private File Checkapkfile (String assetname) {//Put the test apk file under the Assets folder of the project byte[] buffer = new byte[8192];
InputStream is = null;
FileOutputStream fos = null;
try {is = Getassets (). open (Assetname);
FOS = Openfileoutput ("helloactivity.apk", context.mode_world_readable);
int n;
while ((n = is.read (buffer)) >= 0) {fos.write (buffer, 0, N);
}} catch (IOException e) {e.printstacktrace (); } finally {try {if (is = = null) {is.close ();
}} catch (IOException e) {} try {if (fos! = null) {
Fos.close ();
}} catch (IOException e) {}} return Getfilestreampath ("helloactivity.apk");
} private void Showtoastmsg (String msg) {Toast.maketext (this, MSG, Toast.length_short). Show ();
}
}
Layout file
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:gravity=" Center_hori Zontal "android:orientation=" vertical "android:padding=" 10dip "> <textview android:layout_width = "Match_parent" android:layout_height= "wrap_content" android:paddingbottom= "4dip" android:text= "Demo How to install an application using the app installation package and intent intent. "Android:textappearance="? Android:attr/textappearancelarge "/> <button android:id=" @+id/unknow N_source "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:layout_
margintop= "10DP" android:text= "Install apk" > </Button> <button android:id= "@+id/my_source" Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_margintop = "10dP "android:text=" install and Prompt for results "> </Button> <button android:id=" @+id/replace "Andro
Id:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_margintop= "10DP" android:text= "Replace apk" > </Button> <button android:id= "@+id/uninstall" android:layout _width= "Match_parent" android:layout_height= "wrap_content" android:layout_margintop= "10DP" Android : text= "Uninstall apk" > </Button> <button android:id= "@+id/uninstall_result" Android:layout_w Idth= "Match_parent" android:layout_height= "wrap_content" android:layout_margintop= "10DP" android:t ext= "Uninstall and prompt results" > </Button> </LinearLayout>
* * Tip: To lower an installable helloactivity.apk file in the Assets folder of your project, you will not see the effect.
Open Source code: Https://github.com/ldm520/ANDROID_API_DEMOS