nuark hat die Gist bearbeitet . Zu Änderung gehen
1 file changed, 86 insertions
MainActivity.kt(Datei erstellt)
| @@ -0,0 +1,86 @@ | |||
| 1 | + | package xyz.nuark.bgopt | |
| 2 | + | ||
| 3 | + | import android.content.Intent | |
| 4 | + | import android.net.Uri | |
| 5 | + | import android.os.Build | |
| 6 | + | import android.os.Bundle | |
| 7 | + | import android.os.PowerManager | |
| 8 | + | import android.provider.Settings | |
| 9 | + | import android.util.Log | |
| 10 | + | import androidx.activity.enableEdgeToEdge | |
| 11 | + | import androidx.appcompat.app.AppCompatActivity | |
| 12 | + | import androidx.core.view.ViewCompat | |
| 13 | + | import androidx.core.view.WindowInsetsCompat | |
| 14 | + | import xyz.nuark.bgopt.databinding.ActivityMainBinding | |
| 15 | + | import java.util.Timer | |
| 16 | + | import java.util.TimerTask | |
| 17 | + | ||
| 18 | + | class MainActivity : AppCompatActivity() { | |
| 19 | + | private lateinit var binding: ActivityMainBinding | |
| 20 | + | private val timer = Timer() | |
| 21 | + | ||
| 22 | + | override fun onCreate(savedInstanceState: Bundle?) { | |
| 23 | + | super.onCreate(savedInstanceState) | |
| 24 | + | enableEdgeToEdge() | |
| 25 | + | ||
| 26 | + | binding = ActivityMainBinding.inflate(layoutInflater) | |
| 27 | + | setContentView(binding.root) | |
| 28 | + | ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> | |
| 29 | + | val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) | |
| 30 | + | v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) | |
| 31 | + | insets | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | binding.requestOpt.setOnClickListener { | |
| 35 | + | batteryOptimization() | |
| 36 | + | } | |
| 37 | + | ||
| 38 | + | timer.schedule(object : TimerTask() { | |
| 39 | + | override fun run() { | |
| 40 | + | val opt = checkBatteryOptimization() | |
| 41 | + | ||
| 42 | + | binding.root.post { | |
| 43 | + | if (opt) { | |
| 44 | + | binding.optStatus.text = "Optimization is disabled" | |
| 45 | + | } else { | |
| 46 | + | binding.optStatus.text = "Optimization is enabled" | |
| 47 | + | } | |
| 48 | + | } | |
| 49 | + | } | |
| 50 | + | }, 0, 1000) | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | fun batteryOptimization() { | |
| 54 | + | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| 55 | + | val packageName: String? = getPackageName() | |
| 56 | + | val powerManager: PowerManager = getSystemService(POWER_SERVICE) as PowerManager | |
| 57 | + | ||
| 58 | + | if (!powerManager.isIgnoringBatteryOptimizations(packageName)) { | |
| 59 | + | try { | |
| 60 | + | val intent = Intent() | |
| 61 | + | intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) | |
| 62 | + | intent.setData(Uri.parse("package:" + packageName)) | |
| 63 | + | startActivity(intent) | |
| 64 | + | } catch (e: Exception) { | |
| 65 | + | Log.e("BatteryOpt", "Error excluding app from battery optimization", e) | |
| 66 | + | } | |
| 67 | + | } else { | |
| 68 | + | Log.d("BatteryOpt", "App already excluded from battery optimization") | |
| 69 | + | } | |
| 70 | + | } | |
| 71 | + | } | |
| 72 | + | ||
| 73 | + | fun checkBatteryOptimization(): Boolean { | |
| 74 | + | return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| 75 | + | val powerManager: PowerManager = getSystemService(POWER_SERVICE) as PowerManager | |
| 76 | + | val isIgnoringBattery: Boolean = | |
| 77 | + | powerManager.isIgnoringBatteryOptimizations(getPackageName()) | |
| 78 | + | Log.d("BatteryOpt", "Exclusion of battery optimization: " + isIgnoringBattery) | |
| 79 | + | ||
| 80 | + | isIgnoringBattery | |
| 81 | + | } else { | |
| 82 | + | Log.d("BatteryOpt", "No battery optimization") | |
| 83 | + | false | |
| 84 | + | } | |
| 85 | + | } | |
| 86 | + | } | |
Neuer
Älter