1.判断安装文件是否存在
private boolean fileIsExists(){
boolean isExists=false;
try{
String fileName= Environment.getExternalStorageDirectory() +APKpath+apk_name;
File file = new File(fileName);
if(file.exists()){
isExists = true;
}
}catch (Exception e) {
// TODO: handle exception
}
return isExists;
}
2.自动安装
private void InstallAPK() {
try {
File file = new File(Environment.getExternalStorageDirectory()+APKpath, apk_name);
if(!fileIsExists())
{
Toast.makeText(getApplicationContext(), "对不起,更新的文件找不到!", Toast.LENGTH_SHORT).show();
return ;
}
Intent intent = new Intent();
//执行动作
intent.setAction(Intent.ACTION_VIEW);
//为这个新apk开启一个新的activity栈
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//执行的数据类型
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
//开始安装
startActivity(intent);
//关闭旧版本的应用程序的进程
//android.os.Process.killProcess(android.os.Process.myPid());
} catch (Exception e) {
Log.e(TAG,"安装失败");
e.printStackTrace();
}
}