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();
//執(zhí)行動作
intent.setAction(Intent.ACTION_VIEW);
//為這個新apk開啟一個新的activity棧
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//執(zhí)行的數(shù)據(jù)類型
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();
}
}
最后編輯于 :
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者