android 安装assets下的apk

来源:互联网 发布:机智软件破解版 编辑:程序博客网 时间:2024/05/16 07:40

判断手机已安装某程序的方法:
private boolean isAvilible(Context context, StringpackageName){
final PackageManager packageManager =context.getPackageManager();//获取packagemanager
List< packageInfo> pinfo =packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息
List pName = new ArrayList();//用于存储所有已安装程序的包名
//从pinfo中将包名字逐一取出,压入pName list中
if(pinfo != null){
for(int i = 0; i < pinfo.size(); i++){
String pn = pinfo.get(i).packageName;
pName.add(pn);
}
}
returnpName.contains(packageName);//判断pName中是否有目标程序的包名,有TRUE,没有FALSE
}

判断后的逻辑:
//已安装,打开程序,需传入参数包名:"com.skype.android.verizon"

//安装程序,一般是先把apk放到assets目录下,安装时,先将文件copy到sd卡或其它实际目录下,再通过程序来安装
if(isAvilible(this, "com.skype.android.verizon")){

if(copyApkFromAssets(this, "test.apk", Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.apk")){  
  Intent intent = newIntent(Intent.ACTION_VIEW); 
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
  intent.setDataAndType(Uri.parse("file://" +Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.apk"), 
                "application/vnd.android.package-archive"); 
 mContext.startActivity(intent); 

}
}
//未安装,跳转至market下载该程序
else {
Uri uri =Uri.parse("market://details?id=com.skype.android.verizon");//id为包名
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
}

public boolean copyApkFromAssets(Context context,String fileName, String path) { 
   boolean copyIsFinish =false; 
   try { 
    InputStreamis = context.getAssets().open(fileName); 
    File file =new File(path); 
   file.createNewFile(); 
   FileOutputStream fos = newFileOutputStream(file); 
    byte[] temp= new byte[1024]; 
    int i =0; 
    while ((i =is.read(temp)) > 0) { 
    fos.write(temp, 0, i); 
   
   fos.close(); 
   is.close(); 
    copyIsFinish= true; 
   } catch (IOException e){ 
   e.printStackTrace(); 
  
   returncopyIsFinish; 
  }

 

0 0
原创粉丝点击