1. 程式人生 > >應用中對APK進行安裝

應用中對APK進行安裝

ddc his uil strong package file ges server pan

權限

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>   //允許安裝未知來源的app
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
清單文件
<application>
  ................

  <!--適配7.0以上安裝app-->
  <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" /> //說明:這裏的
filepaths是res下面新建的xml名稱的文件夾下的文件
  </provider>
</application>

res資源文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path path="." name="download" /> //說明:name的值是.apk文件的上一級目錄的名稱(不確定,但是跟.apk文件路徑有關系,不是隨便寫的)。
</paths>
</resources>

技術分享圖片

下載    
private void loadFromServer() {
DialogUtil.getInstance().showDialogText(this,"0%");
OkGo.<File>get(appurl)
.tag(this)
.execute(new FileCallback() {
@Override
public void onSuccess(Response<File> response) {
dialogDismiss();
installApp(response.body());
}

@Override
public void onError(Response<File> response) {
super.onError(response);
if (newVersionBean!=null&&newVersionBean.getData()!=null&&"1".equals(newVersionBean.getData().getIs_force_update())){
android.os.Process.killProcess(android.os.Process.myPid()); //獲取PID
System.exit(0);
}else{
dialogDismiss();
displayMessage(getResources().getString(R.string.prompt_downloadfauil));
}
}

@Override
public void downloadProgress(Progress progress) {
super.downloadProgress(progress);
DialogUtil.getInstance().showContent((int)((progress.currentSize/(float)progress.totalSize)*100)+"%");
}
});
}

安裝app
private void installApp(File file){
// setPermission(file.getPath());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("name", "");
intent.addCategory("android.intent.category.DEFAULT");
String packageName = getPackageName();
Uri data;
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){ //8.0以上需要在清單文件中寫上權限:REQUEST_INSTALL_PACKAGES。小米的時候並不需要getPackageManager().canRequestPackageInstalls()的結果為true才做安裝操作,會自動彈出允許安裝未知來源的App的系統彈窗
//            boolean b = getPackageManager().canRequestPackageInstalls();
// 臨時允許
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
data = FileProvider.getUriForFile(this, packageName + ".fileprovider", file);
}else if (Build.VERSION.SDK_INT >= 24){
// 臨時允許
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
data = FileProvider.getUriForFile(this, packageName + ".fileprovider", file);
}else {
data = Uri.fromFile(file);
}
intent.setDataAndType(data, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
/**
* 提升讀寫權限
*
* @param filePath 文件路徑
*/
private static void setPermission(String filePath) {
String command = "chmod " + "777" + " " + filePath;
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}

// 獲取當前App版本
private String getLocalVersion() {
// 獲取包管理者對象
PackageManager pm = getPackageManager();
try {
// 獲取包的詳細信息
PackageInfo info = pm.getPackageInfo(getPackageName(), 0);
// 獲取版本號和版本名稱
return info.versionName;
} catch (Exception e) {
return "";
}
}




 

應用中對APK進行安裝