修复应用初始设置问题,添加应用权限申请。
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Wed Oct 01 21:03:35 HKT 2025
|
||||
#Wed Oct 01 13:21:26 GMT 2025
|
||||
stageCount=6
|
||||
libraryProject=
|
||||
baseVersion=15.0
|
||||
publishVersion=15.0.5
|
||||
buildCount=0
|
||||
buildCount=3
|
||||
baseBetaVersion=15.0.6
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cc.winboll.studio.positions;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
@@ -9,7 +10,10 @@ import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.Switch;
|
||||
import android.widget.Toast;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
|
||||
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
@@ -17,7 +21,6 @@ import cc.winboll.studio.positions.activities.LocationActivity;
|
||||
import cc.winboll.studio.positions.activities.WinBoLLActivity;
|
||||
import cc.winboll.studio.positions.services.MainService;
|
||||
import cc.winboll.studio.positions.utils.AppConfigsUtil;
|
||||
import com.hjq.toast.ToastUtils;
|
||||
|
||||
/**
|
||||
* 主页面:仅负责
|
||||
@@ -27,9 +30,10 @@ import com.hjq.toast.ToastUtils;
|
||||
*/
|
||||
public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
public static final String TAG = "MainActivity";
|
||||
// 位置权限请求码(自定义唯一标识)
|
||||
private static final int REQUEST_LOCATION_PERMISSIONS = 1001;
|
||||
|
||||
// 权限请求码(建议定义为类常量,避免魔法值)
|
||||
private static final int REQUEST_LOCATION_PERMISSIONS = 1001;
|
||||
private static final int REQUEST_BACKGROUND_LOCATION_PERMISSION = 1002;
|
||||
|
||||
// UI 控件:服务控制开关、顶部工具栏
|
||||
private Switch mServiceSwitch;
|
||||
private Button mManagePositionsButton;
|
||||
@@ -37,7 +41,7 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
// 服务相关:服务实例、绑定状态标记
|
||||
//private DistanceRefreshService mDistanceService;
|
||||
private boolean isServiceBound = false;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Activity getActivity() {
|
||||
@@ -120,7 +124,7 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
private void initViews() {
|
||||
mServiceSwitch = (Switch) findViewById(R.id.switch_service_control); // 显式强转
|
||||
mServiceSwitch.setChecked(AppConfigsUtil.getInstance(this).isEnableMainService(true));
|
||||
|
||||
|
||||
mManagePositionsButton = (Button) findViewById(R.id.btn_manage_positions);
|
||||
mManagePositionsButton.setEnabled(mServiceSwitch.isChecked());
|
||||
|
||||
@@ -206,49 +210,57 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
return hasForegroundPerm && hasBackgroundPerm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请「前台+后台」位置权限(弹出系统权限弹窗,适配版本差异)
|
||||
*/
|
||||
private void requestLocationPermissions() {
|
||||
String[] permissions;
|
||||
// Java7 显式版本判断:Android 10+ 需同时申请前台+后台权限
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
permissions = new String[]{
|
||||
android.Manifest.permission.ACCESS_FINE_LOCATION,
|
||||
android.Manifest.permission.ACCESS_BACKGROUND_LOCATION
|
||||
};
|
||||
} else {
|
||||
// 低版本仅需申请前台权限
|
||||
permissions = new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION};
|
||||
}
|
||||
// 触发系统权限申请(Java7 显式调用requestPermissions,无方法引用)
|
||||
requestPermissions(permissions, REQUEST_LOCATION_PERMISSIONS);
|
||||
}
|
||||
private void requestLocationPermissions() {
|
||||
// 1. 先判断前台定位权限(ACCESS_FINE_LOCATION)是否已授予
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
// 1.1 未授予前台权限:先申请前台权限(API 30+ 后台权限依赖前台权限)
|
||||
String[] foregroundPermissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
|
||||
// 对API 23+(Android 6.0)动态申请,低版本会直接授予(清单已声明前提下)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
requestPermissions(foregroundPermissions, REQUEST_LOCATION_PERMISSIONS);
|
||||
}
|
||||
} else {
|
||||
// 2. 已授予前台权限:判断是否需要申请后台权限(仅API 29+需要)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
// 2.1 检查后台权限是否未授予
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
// 2.2 API 30+ 必须单独申请后台权限(不能和前台权限一起弹框)
|
||||
requestPermissions(
|
||||
new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},
|
||||
REQUEST_BACKGROUND_LOCATION_PERMISSION
|
||||
);
|
||||
}
|
||||
}
|
||||
// 3. 前台权限已授予(+ 后台权限按需授予):此处可执行定位相关逻辑
|
||||
// doLocationRelatedLogic();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理权限申请结果(系统弹窗用户选择后回调,Java7 显式重写方法)
|
||||
*/
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
// 仅处理位置权限的申请结果
|
||||
if (requestCode == REQUEST_LOCATION_PERMISSIONS) {
|
||||
boolean allGranted = true;
|
||||
// Java7 显式for循环遍历结果(无增强for简化写法)
|
||||
for (int i = 0; i < grantResults.length; i++) {
|
||||
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
|
||||
allGranted = false;
|
||||
break; // 有一个权限未通过,直接终止判断
|
||||
}
|
||||
}
|
||||
// 【必须补充】权限申请结果回调(处理用户同意/拒绝逻辑)
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
// 处理前台权限申请结果
|
||||
if (requestCode == REQUEST_LOCATION_PERMISSIONS) {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
// 前台权限同意:自动尝试申请后台权限(如果是API 29+)
|
||||
requestLocationPermissions();
|
||||
} else {
|
||||
// 前台权限拒绝:提示用户(可选:引导跳转到应用设置页)
|
||||
Toast.makeText(this, "需要前台定位权限才能使用该功能", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else if (requestCode == REQUEST_BACKGROUND_LOCATION_PERMISSION) {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
// 后台权限同意:可执行后台定位逻辑
|
||||
Toast.makeText(this, "已获得后台定位权限", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
// 后台权限拒绝:提示用户(可选:说明后台定位的用途,引导手动开启)
|
||||
Toast.makeText(this, "拒绝后台权限将无法在后台持续定位", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 提示用户权限结果,并处理开关状态
|
||||
if (allGranted) {
|
||||
ToastUtils.show("已获取位置权限(含后台),可正常使用定位功能");
|
||||
} else {
|
||||
ToastUtils.show("位置权限未完全授予,无法后台获取GPS数据,请在设置中开启");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -53,11 +53,14 @@ public class AppConfigsUtil {
|
||||
if (isReloadConfigs) {
|
||||
loadConfigs();
|
||||
}
|
||||
|
||||
return mAppConfigsModel.isEnableMainService();
|
||||
|
||||
return (mAppConfigsModel == null) ?false: mAppConfigsModel.isEnableMainService();
|
||||
}
|
||||
|
||||
public void setIsEnableMainService(boolean isEnableMainService) {
|
||||
if(mAppConfigsModel == null) {
|
||||
mAppConfigsModel = new AppConfigsModel();
|
||||
}
|
||||
mAppConfigsModel.setIsEnableMainService(isEnableMainService);
|
||||
saveConfigs();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user