Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
修复应用更新操作部分权限问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacknic committed May 26, 2019
1 parent 505126a commit d8becba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!-- 用于崩溃信息收集 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<!-- /用于崩溃信息收集 -->
<application
android:name=".App"
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/java/com/jacknic/glut/util/UpdateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
Expand Down Expand Up @@ -99,7 +100,7 @@ public void onError(Call call, Response response, Exception e) {
*
* @param url 更新包下载地址
*/
public static void downloadApk(String url, final Activity activity) {
public static void downloadApk(final String url, final Activity activity) {
final AlertDialog dialog = new AlertDialog.Builder(activity)
.setTitle("更新包下载中")
.setMessage("下载进度... 0%")
Expand All @@ -111,19 +112,30 @@ public void onClick(DialogInterface dialog, int which) {
})
.setPositiveButton("后台下载", null)
.create();
OkGo.get(url).execute(new FileCallback(activity.getExternalCacheDir().toString(), "glut_update.apk") {
OkGo.get(url).execute(new FileCallback(activity.getCacheDir().getAbsolutePath(), "glut_update.apk") {
@Override
public void onBefore(BaseRequest request) {
dialog.show();
}

@Override
public void onError(Call call, Response response, Exception e) {
super.onError(call, response, e);
if (response != null && response.isRedirect()) {
String location = response.header("Location");
if (!TextUtils.isEmpty(location)) {
downloadApk(location, activity);
}
}
}

@Override
public void onSuccess(File file, Call call, Response response) {
Intent intent = new Intent(Intent.ACTION_VIEW);
String type = "application/vnd.android.package-archive";
//判断是否是AndroidN以及更高的版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".fileProvider", file);
intent.setDataAndType(contentUri, type);
} else {
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="files_root"
path="Android/data/com.jacknic.glut/" />
<!--分享内部cache文件-->
<cache-path
name="cache"
path="/" />
</paths>

0 comments on commit d8becba

Please sign in to comment.