存储文件权限

This commit is contained in:
jia 2024-09-02 21:08:03 +08:00
parent 28adf72331
commit 171d0489c2
2 changed files with 146 additions and 5 deletions

View File

@ -1,5 +1,7 @@
package com.ifish.activity;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
@ -20,11 +22,14 @@ import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import com.ifish.basebean.BaseBean;
import com.ifish.basebean.FinishMainActivity;
import com.ifish.basebean.Version;
import com.ifish.baseclass.BaseActivity;
import com.ifish.baseclass.UriForFile;
import com.ifish.permission.PermissionHelper;
import com.ifish.utils.ActivityManager;
import com.ifish.utils.AnimationUtil;
import com.ifish.utils.Commons;
@ -37,6 +42,7 @@ import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.util.LogUtils;
import com.p2p.core.P2PHandler;
import java.io.File;
@ -257,8 +263,137 @@ public class MySettingActivity extends BaseActivity {
/**
* 弹出下载进度条 开始下载apk
*/
// private void showDownloadApkDialog() {
// apkDownloadDialog = new Dialog(MySettingActivity.this, R.style.DefautDialogs);
// apkDownloadDialog.setCancelable(false);
// apkDownloadDialog.show();
// Window window = apkDownloadDialog.getWindow();
// window.setContentView(R.layout.dialog_downloadapk);
// final TextView tv_title = (TextView) window.findViewById(R.id.tv_title);
// final TextView tv_current = (TextView) window.findViewById(R.id.tv_current);
// final ProgressBar progess_bar = (ProgressBar) window.findViewById(R.id.progess_bar);
// final Button bt_redownload = (Button) window.findViewById(R.id.bt_redownload);
// final String downloaded = "maybe the file has downloaded completely";//如果文件已经下载好 就会抛出此异常 可以判断此异常直接打开文件
// /**
// * 下载apk
// */
// L.i("====================appAddress=" + appAddress + "+path=" + apkPath);
// http.download(appAddress, apkPath, false, false, new RequestCallBack<File>() {
// @Override
// public void onStart() {
// super.onStart();
// tv_title.setText("开始下载...");
// tv_current.setText("0%");
// bt_redownload.setVisibility(View.GONE);//开始下载时 把重新下载按钮隐藏
// progess_bar.setProgress(0);//进度条初始
// }
//
// @Override
// public void onLoading(long total, long current, boolean isUploading) {
// super.onLoading(total, current, isUploading);
// progess_bar.setProgress((int) ((double) current / (double) total * 100));
// double currentA = (double) (current) / (1024 * 1024);//byte转MB除以1024*1024
// double totaldB = (double) total / (1024 * 1024);
// String formatA = String.format("%.2f", currentA);//只保留2位小数
// String formatB = String.format("%.2f", totaldB);
// tv_title.setText("下载中 " + formatA + "MB/" + formatB + "MB");
// tv_current.setText((int) (current * 100 / total) + "%");
// }
//
// @Override
// public void onFailure(HttpException arg0, String msg) {
// if (downloaded.equals(msg)) {//已经存在要下载的文件 会抛出此异常 可以直接安装
// progess_bar.setProgress(100);//进度条填满
// tv_title.setText("安装包已存在");
// apkFile = new File(apkPath);
// installApk();
// } else {
// ToastUtil.show(MySettingActivity.this, Commons.Text.ERROR);
// bt_redownload.setVisibility(View.VISIBLE); //网络错误下载失败时 把重新下载按钮显示
// }
// }
//
// @Override
// public void onSuccess(ResponseInfo<File> arg0) {
// bt_redownload.setVisibility(View.GONE);//下载成功 把重新下载按钮隐藏 并跳转安装apk
// tv_title.setText("安装包下载成功");
// apkFile = arg0.result;
// installApk();
// }
// });
// bt_redownload.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// delApk();//删除apk
// bt_redownload.setVisibility(View.GONE);//开始下载时 把重新下载按钮隐藏
// http.download(appAddress, apkPath, false, false, new RequestCallBack<File>() {
// @Override
// public void onStart() {
// super.onStart();
// tv_title.setText("开始下载...");
// tv_current.setText("0%");
// bt_redownload.setVisibility(View.GONE);//开始下载时 把重新下载按钮隐藏
// progess_bar.setProgress(0);//进度条初始
// }
//
// @Override
// public void onLoading(long total, long current, boolean isUploading) {
// super.onLoading(total, current, isUploading);
// progess_bar.setProgress((int) ((double) current / (double) total * 100));
// double currentA = (double) (current) / (1024 * 1024);//byte转MB除以1024*1024
// double totaldB = (double) total / (1024 * 1024);
// String formatA = String.format("%.2f", currentA);//只保留2位小数
// String formatB = String.format("%.2f", totaldB);
// tv_title.setText("下载中 " + formatA + "MB/" + formatB + "MB");
// tv_current.setText((int) (current * 100 / total) + "%");
// }
//
// @Override
// public void onFailure(HttpException arg0, String msg) {
// if (downloaded.equals(msg)) {//已经存在要下载的文件 会抛出此异常 可以直接安装
// progess_bar.setProgress(100);//进度条填满
// tv_title.setText("安装包已存在");
// apkFile = new File(apkPath);
// installApk();
// } else {
// ToastUtil.show(MySettingActivity.this, Commons.Text.ERROR);
// bt_redownload.setVisibility(View.VISIBLE); //网络错误下载失败时 把重新下载按钮显示
// }
// }
//
// @Override
// public void onSuccess(ResponseInfo<File> arg0) {
// bt_redownload.setVisibility(View.GONE);//下载成功 把重新下载按钮隐藏 并跳转安装apk
// tv_title.setText("安装包下载成功");
// apkFile = arg0.result;
// installApk();
// }
// });
// }
// });
// }
private void showDownloadApkDialog() {
apkDownloadDialog = new Dialog(MySettingActivity.this, R.style.DefautDialogs);
LogUtils.d("LHD 展示下载框");
//获取权限
boolean hasPermission = PermissionHelper.checkFilePermission(this);
LogUtils.d("LHD 是否具有文件管理权限:" + hasPermission);
if (!hasPermission) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// LogUtils.d("LHD android 13不需要请求");
// } else
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//请求文件系统权限
LogUtils.d("LHD 请求MANAGE_EXTERNAL_STORAGE权限");
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s", this.getApplicationContext().getPackageName())));
startActivityForResult(intent, 301);
} else {
ActivityCompat.requestPermissions(this, new String[]{WRITE_EXTERNAL_STORAGE}, 302);
}
}
apkDownloadDialog = new Dialog(this, R.style.DefautDialogs);
apkDownloadDialog.setCancelable(false);
apkDownloadDialog.show();
Window window = apkDownloadDialog.getWindow();
@ -296,6 +431,7 @@ public class MySettingActivity extends BaseActivity {
@Override
public void onFailure(HttpException arg0, String msg) {
L.i(msg);
if (downloaded.equals(msg)) {//已经存在要下载的文件 会抛出此异常 可以直接安装
progess_bar.setProgress(100);//进度条填满
tv_title.setText("安装包已存在");
@ -366,7 +502,6 @@ public class MySettingActivity extends BaseActivity {
}
});
}
/**
* 重新下载文件之前 需要删除已存在的apk 否则出现安装包解析错误
*/

View File

@ -166,6 +166,7 @@ public class DeviceFragment extends BaseV4Fragment implements ObservableScrollVi
private HttpManager hm = HttpManager.getInstance();
private String appAddress = "";
private String versionCode = "";
private String spversionCode = "";
private String uploadContent = "";
private String isMustUpdate = "";
private String MustUpdate = "1";
@ -624,7 +625,12 @@ public class DeviceFragment extends BaseV4Fragment implements ObservableScrollVi
private void checkVersion() {
versionCode = versionCode.toUpperCase();
if (!HttpManager.getVersionName(getActivity()).equals(versionCode)) {
L.i("jjiaa------版本号"+versionCode);
spversionCode = sp.getString("versionCode","");
if (!HttpManager.getVersionName(getActivity()).equals(versionCode) && !spversionCode.equals(versionCode)) {
if (!MustUpdate.equals(isMustUpdate)) {
sp.putString("versionCode",versionCode);
}
final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setTitle("发现新版本,是否升级?");
dialog.setCancelable(false); //返回键不能取消对话框 强制更新