码迷,mamicode.com
首页 > 其他好文 > 详细

应用中对APK进行安装

时间:2018-12-29 19:38:31      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:dia   his   param   message   路径   image   request   ogre   分享图片   

权限

<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进行安装

标签:dia   his   param   message   路径   image   request   ogre   分享图片   

原文地址:https://www.cnblogs.com/qynprime/p/10197482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!