mirror of
https://gitee.com/willfree/min-vpn-client_v2.git
synced 2026-10-04 19:42:00 +08:00
修复在一些更新的安卓手机机型中出现的版本更新问题,解决方法为通过系统设置,请求所有文件访问权限
This commit is contained in:
1 parent
089652aa08
commit
7a9925482c
6 files changed
+47
-7
No files matched your search
+1
-1
@@ -43,7 +43,7 @@ android {
|
||||
minSdkVersion 24
|
||||
targetSdkVersion 30
|
||||
versionCode 2
|
||||
versionName "2.1.2"
|
||||
versionName "2.1.3"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
|
||||
|
||||
@@ -67,6 +67,8 @@
|
||||
<!-- 往SDCard写入数据权限 -->
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||
tools:ignore="ScopedStorage" />
|
||||
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
|
||||
tools:ignore="ScopedStorage" />
|
||||
<!-- 以下为copy旧版本VPN的权限定义部分 -->
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
|
||||
@@ -17,6 +17,8 @@ import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.google.android.material.checkbox.MaterialCheckBox;
|
||||
import com.pkusz.min_vpn_client.GlobalSetting;
|
||||
import com.pkusz.min_vpn_client.R;
|
||||
@@ -239,7 +241,7 @@ public class LoginActivity extends MINVpnBaseActivity<LoginActivityPresenter>
|
||||
return null;
|
||||
});
|
||||
|
||||
// todo: 测试是否需要更新版本
|
||||
// 测试是否需要更新版本
|
||||
UpdateAppUtils.checkUpdate(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ public class ListActivity extends AppCompatActivity {
|
||||
alertInfo("请输入有效的手机号");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// todo: 这里默认是中国的手机号。后期可写成更高级的形式去兼容国外号码
|
||||
GetVerificationCode smsRequest=new GetVerificationCode(rawUsername,
|
||||
"86"+phoneNumber);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.pkusz.min_vpn_client.utils.updateApp;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.ProgressDialog;
|
||||
@@ -15,6 +16,7 @@ import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
@@ -22,7 +24,6 @@ import android.widget.Toast;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
@@ -51,7 +52,7 @@ public class UpdateAppUtils {
|
||||
private ProgressDialog progressDialog;//打圈框
|
||||
private ProgressDialog progressDialog_update;//升级进度框
|
||||
private Handler handler = new Handler() {
|
||||
@SuppressLint("HandlerLeak")
|
||||
@SuppressLint(value = "HandlerLeak")
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
@@ -64,7 +65,7 @@ public class UpdateAppUtils {
|
||||
break;
|
||||
case 1://下载失败
|
||||
dismissUpdateDialog();
|
||||
showToast("下载失败,请稍后重试");
|
||||
showToast("下载失败,请稍后重试" + errorDownloadString + openAccessInfo);
|
||||
break;
|
||||
case 2://正在下载
|
||||
showToast("正在下载中");
|
||||
@@ -81,7 +82,15 @@ public class UpdateAppUtils {
|
||||
}
|
||||
};
|
||||
|
||||
// 提示打开所有文件权限访问
|
||||
private String openAccessInfo="\n 重启本APP,如跳转到系统设置界面," +
|
||||
"请打开本APP的\"所有文件访问权限\",以支持新版本的下载与安装。" +
|
||||
"如操作无效,请联系开发者,并提供您的失败原因码。";
|
||||
|
||||
public static void checkUpdate(Context context){
|
||||
// 每次检查更新,都先看是否有更新所需的存储权限
|
||||
checkStorageManagerPermission(context);
|
||||
// 检查更新
|
||||
String infoUrl="https://minvpn.qjm253.cn/ztc/android/version.txt";
|
||||
String apkUrl="https://minvpn.qjm253.cn/ztc/android/MIN_VPN_v";
|
||||
UpdateAppUtils versionUtils=new UpdateAppUtils(context,true,infoUrl,apkUrl);
|
||||
@@ -348,6 +357,17 @@ public class UpdateAppUtils {
|
||||
startDownloadApp();
|
||||
}
|
||||
|
||||
// 参考实现:https://blog.csdn.net/qq_17766199/article/details/115351949
|
||||
public static void checkStorageManagerPermission(Context context) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R &&
|
||||
!Environment.isExternalStorageManager()) {
|
||||
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
private String errorDownloadString ="\n 失败原因码:";
|
||||
/**
|
||||
* 开始下载
|
||||
*/
|
||||
@@ -356,6 +376,7 @@ public class UpdateAppUtils {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
errorDownloadString +="DownloadApp-1";
|
||||
long totalSize;// 文件总大小
|
||||
long downloadCount = 0;// 已经下载好的大小
|
||||
// String path;
|
||||
@@ -371,30 +392,40 @@ public class UpdateAppUtils {
|
||||
// filePath.mkdirs();
|
||||
// }
|
||||
|
||||
errorDownloadString +="-2";
|
||||
// file对象,注意路径要和resource xml里配置的一样
|
||||
File apkPath = new File(Environment.getExternalStorageDirectory()+File.separator+"myapk", "app.apk");
|
||||
errorDownloadString +="-3";
|
||||
String path=Environment.getExternalStorageDirectory()+File.separator+"myapk";
|
||||
errorDownloadString +="-4";
|
||||
File filePath=new File(path);
|
||||
if (!filePath.exists() || !filePath.isDirectory()) {
|
||||
filePath.mkdirs();
|
||||
}
|
||||
errorDownloadString +="-5";
|
||||
|
||||
// File apkPath = new File(filePath, "app.apk");
|
||||
if (!apkPath.exists()) {
|
||||
apkPath.createNewFile();
|
||||
}
|
||||
errorDownloadString +="-6";
|
||||
|
||||
URL url = new URL(apkurl);
|
||||
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
|
||||
errorDownloadString +="-7";
|
||||
httpURLConnection.setConnectTimeout(10 * 1000);
|
||||
httpURLConnection.setReadTimeout(10 * 1000);
|
||||
httpURLConnection.setDoInput(true);
|
||||
// 获取下载文件的size
|
||||
totalSize = httpURLConnection.getContentLength();
|
||||
errorDownloadString +="-8";
|
||||
int response = httpURLConnection.getResponseCode(); //获得服务器的响应码
|
||||
errorDownloadString +="-9";
|
||||
if (response == HttpURLConnection.HTTP_OK) {//200
|
||||
errorDownloadString +="-10";
|
||||
InputStream inputStream = httpURLConnection.getInputStream();
|
||||
FileOutputStream outputStream = new FileOutputStream(apkPath, false);// 文件存在则覆盖掉
|
||||
errorDownloadString +="-11";
|
||||
byte buffer[] = new byte[1024];
|
||||
int readsize = 0;
|
||||
while ((readsize = inputStream.read(buffer)) != -1) {
|
||||
@@ -407,23 +438,28 @@ public class UpdateAppUtils {
|
||||
handler.sendMessage(msg);
|
||||
Log.i(TAG, "downloadCount:" + downloadCount);
|
||||
}
|
||||
errorDownloadString +="-12";
|
||||
|
||||
httpURLConnection.disconnect();
|
||||
outputStream.flush();
|
||||
inputStream.close();
|
||||
outputStream.close();
|
||||
|
||||
errorDownloadString +="-13";
|
||||
Message msg = handler.obtainMessage();
|
||||
msg.what = 0;
|
||||
msg.obj = apkPath;
|
||||
handler.sendMessage(msg);
|
||||
|
||||
errorDownloadString +="-14";
|
||||
Log.i(TAG, apkPath + " downloadCount:" + downloadCount);
|
||||
} else {
|
||||
errorDownloadString +="-00";
|
||||
handler.sendEmptyMessage(1);
|
||||
Log.i(TAG, "httpErr:response:" + response);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
errorDownloadString +="-0";
|
||||
handler.sendEmptyMessage(1);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
android:layout_marginLeft="@dimen/dp_20"
|
||||
android:layout_marginRight="@dimen/dp_0"
|
||||
android:padding="5dp"
|
||||
android:text="v2.1.2"
|
||||
android:text="v2.1.3"
|
||||
android:textColor="@color/color_bg_name"
|
||||
android:textSize="17.5dp"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
|
||||
Reference in new issue
Block a user