when run this code that exception throw 
Intent intent=new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);  //intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);  //intent.setData(Uri.parse("package:" + getPackageName()));

//intent.addCategory(Intent.CATEGORY_ALTERNATIVE); Uri uri = Uri.fromParts("package", getPackageName(), null); intent.setData(uri); startActivity(intent); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);

2022-03-08 22:55:45.172 3715-3715/com.example.batterop E/Error: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS dat=package:com.example.batterop } 
1

There are 1 answers

0
Mori On

Add in the Manifest file:

 <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

In Kotlin use like this:

  @SuppressLint("BatteryLife")
    fun permissionBattery() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val packageName: String = baseContext.packageName
            val pm: PowerManager =
                baseContext.getSystemService(Context.POWER_SERVICE) as PowerManager
            if (!pm.isIgnoringBatteryOptimizations(packageName)) {
                val intent = Intent()
                intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                intent.data = Uri.parse("package:$packageName")
                baseContext.startActivity(intent)
            }

        }


    }