From c50db4ba87c72b542b85a3115b5c726c334c4be1 Mon Sep 17 00:00:00 2001 From: wenyifan Date: Mon, 16 Jan 2023 23:15:24 +0800 Subject: [PATCH] request permissions --- app/src/main/AndroidManifest.xml | 10 ++- .../main/java/run/evan/gost/MainActivity.java | 69 ++++++++++++++++++- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index aae2d55..4f4d9fb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,11 +2,17 @@ - - + + + + + = Build.VERSION_CODES.R && !appPreferences.getBoolean("refuse", false)) { + // 先判断有没有权限 + if (!Environment.isExternalStorageManager()) { + Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION); + intent.setData(Uri.parse("package:" + getPackageName())); + startActivityForResult(intent, 1024); + } + } + + + if (!isIgnoringBatteryOptimizations()) { + requestIgnoreBatteryOptimizations(); + } textView1 = findViewById(R.id.textView); textView1.setMovementMethod(ScrollingMovementMethod.getInstance()); configEditText = findViewById(R.id.configEditText); configPreferences = getSharedPreferences("config", MODE_PRIVATE); - appPreferences = getSharedPreferences("app", MODE_PRIVATE); sp = findViewById(R.id.spinner); @@ -290,6 +323,40 @@ public class MainActivity extends AppCompatActivity { } + @RequiresApi(api = Build.VERSION_CODES.M) + private void requestIgnoreBatteryOptimizations() { + try { + Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); + intent.setData(Uri.parse("package:" + getPackageName())); + startActivity(intent); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @RequiresApi(api = Build.VERSION_CODES.M) + private boolean isIgnoringBatteryOptimizations() { + boolean isIgnoring = false; + PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); + if (powerManager != null) { + isIgnoring = powerManager.isIgnoringBatteryOptimizations(getPackageName()); + } + return isIgnoring; + } + + + // 带回授权结果 + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == 1024 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + // 检查是否有权限 + if (!Environment.isExternalStorageManager()) { + appPreferences.edit().putBoolean("refuse", true).apply(); + } + } + } + private void reloadConfig() { starAdapter.clear(); Map all = configPreferences.getAll();