Android assests 应用自带apk安装方法

public class main extends Activity {

	private boolean installedMxPlayerFlag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		installedMxPlayerFlag = isAlreadyInstalled(this,
				"com.mxtech.videoplayer.pro");
		if (!installedMxPlayerFlag) {
			String fileDir=Environment.getExternalStorageDirectory()
					.getAbsolutePath() + "/download/";
			String filepath = fileDir+"MX_Player_Pro.apk";
			File apkFile = new File(filepath);
			if (apkFile.exists()) {
				Log.d("apk", "====apkFile.exists=====");
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setDataAndType(Uri.parse("file://" + filepath),
						"application/vnd.android.package-archive");
				startActivity(i);
			} else {
				// write to filepath
				Log.d("apk", "====WriteApkToDisk=====");
				File dir=new File(fileDir);
				if(!dir.exists()){
					dir.mkdirs();
				}
				boolean writeApkFlag = WriteApkToDisk(filepath);
				if (writeApkFlag) {
					Intent i = new Intent(Intent.ACTION_VIEW);
					i.setDataAndType(Uri.parse("file://" + filepath),
							"application/vnd.android.package-archive");
					startActivity(i);
				}else{
					Log.d("apk", "====WriteApkToDisk=====!!!!!!!!!!!!failed");
				}
			}
		}

		Button btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivityFromActivityName("com.mxtech.videoplayer.pro",
						"com.mxtech.videoplayer.ActivityScreen");
			}
		});

	}

	private boolean isAlreadyInstalled(Context context, String packageName) {
		boolean installedMxPlayerFlagTmp = false;
		final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
		List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
		List<String> pName = new ArrayList<String>();// 用于存储所有已安装程序的包名
		// 从pinfo中将包名字逐一取出,压入pName list中
		if (pinfo != null) {
			for (int i = 0; i < pinfo.size(); i++) {
				if (packageName.trim().equalsIgnoreCase(
						pinfo.get(i).packageName)) {
					installedMxPlayerFlagTmp = true;
					Log.d("apk", pinfo.get(i).packageName);

				}
			}
		}
		return installedMxPlayerFlagTmp;// 判断pName中是否有目标程序的包名,有TRUE,没有FALSE
	}

	public boolean WriteApkToDisk(String filepath) {
		boolean writeApkFlag = false;
		try {
			InputStream is = getAssets().open("MX_Player_Pro.apk");
			File file = new File(filepath);
			file.createNewFile();
			FileOutputStream fos = new FileOutputStream(file);
			byte[] temp = new byte[1024];
			int i = 0;
			while ((i = is.read(temp)) > 0) {
				fos.write(temp, 0, i);
			}
			fos.close();
			is.close();
			writeApkFlag = true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			writeApkFlag = false;
		} catch (IOException e) {
			e.printStackTrace();
			writeApkFlag = false;
		}
		return writeApkFlag;
	}

	public void startActivityFromActivityName(String packName,
			String ActivityName) {
		ComponentName cn = new ComponentName(packName, ActivityName);
		Intent i = new Intent();
		String type = "video/* ";
		Uri uri = Uri
				.parse("http://proiptv.iptv101.com:8000/live/testkorax/070215/3.ts");

		i.setDataAndType(uri, type);
		i.setComponent(cn);
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(i);
	}

	PackageManager pm = null;

	public void startActivityFromLauchIntent(String packageName) {
		pm = getPackageManager();
		Intent i = pm.getLaunchIntentForPackage(packageName);
		String type = "video/* ";
		Uri uri = Uri
				.parse("http://trailers.asiamoviepass.eu/amp/hi/bbi-014.flv");

		i.setDataAndType(uri, type);
		if (null != i) {
			startActivity(i);
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值