Android M startActivity battery optimization

前端 未结 3 820
北荒
北荒 2021-02-08 17:30

I\'m developing an app that should alert an user if is near a place. and of course have to do that also if the phone is in idle. With DOZE now I understood that I have to whitel

3条回答
  •  再見小時候
    2021-02-08 17:32

    To open list of apps for choosing battery optimization you can use this code sample:

    private void openPowerSettings(Context context) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
        context.startActivity(intent);
    }
    

    It doesn't need to have permission, so it should be ok to publish it to Google Play (for details please check this thread and comments to this question).

    NOTE

    Adding this line

    intent.setData(Uri.parse("package:" + mContext.getPackageName()));
    

    will cause crash "Fatal Exception: android.content.ActivityNotFoundException No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:io.demo.example }". User has to find the app in the list, it seems no way to jump directly to our app.

提交回复
热议问题