您的位置:首页 > 移动开发 > Android开发

Android assests 应用自带apk安装方法

2015-03-13 16:22 316 查看
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);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐