自動安裝

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();
        }
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容