Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android應用程序的安裝與卸載

android應用程序的安裝與卸載

編輯:關於Android編程

程序的安裝與卸載在實際開發中也很常見,尤其是安裝,在版本更新中必然會有。直接上示例:

MainActivity:

package com.home.installanduninstall;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	private Button installBtn;
	private Button uninstallBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		installBtn = (Button) findViewById(R.id.main_btn_install);
		uninstallBtn = (Button) findViewById(R.id.main_btn_uninstall);
		installBtn.setOnClickListener(this);
		uninstallBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == installBtn) {
			install();
		}
		if (v == uninstallBtn) {
			uninstall();
		}
	}

	/**
	 * 安裝應用程序
	 */
	private void install() {
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			Toast.makeText(this, "請插入SD卡", Toast.LENGTH_SHORT).show();
			return;
		}
		File sdCardDir = Environment.getExternalStorageDirectory();
		File file = new File(sdCardDir, "Sport.apk");
		if (!file.exists()) {
			Toast.makeText(this, "文件未找到", Toast.LENGTH_SHORT).show();
			return;
		}
		Uri uri = Uri.fromFile(file);
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(uri, "application/vnd.android.package-archive");
		startActivity(intent);
	}

	/**
	 * 卸載應用程序
	 */
	private void uninstall() {
		Uri uri = Uri.parse("package:com.lovo.sport");
		Intent intent = new Intent(Intent.ACTION_DELETE, uri);
		startActivity(intent);
	}

}

布局xml:



    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved