Android implements installation and uninstallation of applications in the form of code

Source: Internet
Author: User

Because the application installation and uninstall module has been written on the Android system, so we just need to activate it.

Attention:

Intent.setdataandtype (uri.fromfile (file), "Application/vnd.android.package-archive"); In this sentence, the first parameter is the path to the APK to install. The second parameter is the type corresponding to the APK. Can be found in the Web.xml in the Conf directory under the installation directory of Tomcat.

Program Run screenshot:

The code implementation is as follows:

1, Main.xml

<?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:o" rientation= "vertical" >
    
    <button 
        android:layout_width= "Wrap_content"
        Wrap_content "
        android:text=" Installation "
        android:onclick=" Install "
        />

    <button 
        android: Layout_width= "Wrap_content"
        android:layout_height= "wrap_content"
        android:text= "Uninstall"
        Android:o nclick= "Uninstall"
        />
    
</LinearLayout>

2, Mainactivity

 package Com.njupt.install;

Import Java.io.File;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
Import android.app.Activity;
Import android.content.Intent;
Import Android.view.Menu;

Import Android.view.View; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super.onc
		Reate (savedinstancestate);
	Setcontentview (R.layout.main);
		public void Install (View v) {Intent Intent = new Intent ();
		
		Intent.setaction (Intent.action_view);
		File File = new file (Environment.getexternalstoragedirectory (), "htmlui1.apk");
		
		Intent.setdataandtype (uri.fromfile (file), "application/vnd.android.package-archive");
	StartActivity (Intent);
	    public void Uninstall (View v) {Intent Intent = new Intent ();
	    Intent.setaction (Intent.action_delete);
		Intent.setdata (Uri.parse ("Package:com.njupt.htmlui1"));
	StartActivity (Intent); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds the items to the action bar if it is present.
		Getmenuinflater (). Inflate (R.menu.main, menu);
	return true; }

}
Related Article

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.