android 在应用中安装其他程序

首先需要AndroidManifest.xml中加入安装程序权限:
 <!-- 安装程序权限 -->
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>

第二步把安装程序添加进SDCard。如把文件名为” sogouinput_android_1.40_sweb.apk.zip”的sogou拼音输入法安装文件放进SDCard。

第三步在程序中添加以下代码:
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "sogouinput_android_1.40_sweb.apk.zip")),"application/vnd.android.package-archive");

startActivity(intent);

代码示例:

package cn.itcast.packageinstaller;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class DemoActivity extends Activity {
	private EditText et_path;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		et_path = (EditText) this.findViewById(R.id.et_path);
	}

	public void install(View view) {
		String path = et_path.getText().toString().trim();

		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(new File(path)),
				"application/vnd.android.package-archive");
		startActivity(intent);
	}
}

你可能感兴趣的:(android,代码中安装app)