Android assests 应用自带apk安装方法

来源:互联网 发布:java多线程轮流输出 编辑:程序博客网 时间:2024/05/02 02:27
public class main extends Activity {private boolean installedMxPlayerFlag;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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 filepathLog.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() {@Overridepublic 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();// 获取packagemanagerList<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);}}}

0 0
原创粉丝点击