android - 如何处理用户对 ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 的回答?

标签 android android-intent android-doze

目前,我向用户询问应用程序功能以节省电池电量:

public void startSessionClick(View view) {

        String token = MyApp.getInstance().getPrefToken();
        if (!TextUtils.isEmpty(token)) {
            throw new RuntimeException("Token is not empty");
        }

        //todo hot-fix
        if (checkBatteryOptimization()) {
            Intent intent = new Intent(this, AuthEnterPhoneActivity.class);
            startActivityForResult(intent, REQUEST_CODE_ENTER_PHONE);
        } else {
            String packageName = getApplicationContext().getPackageName();
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
            getApplicationContext().startActivity(intent);
            //handleAnswer();
            startSessionClick(null);
        }
    }

如何跟踪同意或拒绝对话?

在协议(protocol)的那一刻,我只是打开下一个 Activity (在此之前检查库存设置),如果用户拒绝,则再次显示此对话框。

我需要的跟踪是在无法显示带有解释的对话框的情况下。

最佳答案

在您的 Activity 中,您可以覆盖函数 onActivityResult .它将被框架调用并传递一个参数resultCode

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == REQUEST_CODE_ENTER_PHONE) {
         if (resultCode == RESULT_OK) {
             // user accepted the permission request
             ...
         }
     }
https://developer.android.com/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent)

关于android - 如何处理用户对 ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 的回答?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43119217/

相关文章:

android - 广播接收器在 Android 8 的前台服务中不起作用

java - 如何从android中的原始有效负载中提取值(value)?

android - 在android上存储JSON : sqlite vs SharedPreference

java - Android - 如何使用 HttpClient 接受任何证书并同时传递证书

android - 如何在android studio中添加新的导入

android - 是否可以使 Chrome CustomTabs Intent 不打开外部应用程序?

Android 语言环境更改

android - 我尝试在教程中的 setFlags 中使用这个标志,但它已被弃用,我该怎么办

java - 当手机处于打瞌睡模式时扫描蓝牙 LE 设备不起作用。打瞌睡模式时不会调用 CallBack 方法

android - 当应用程序处于后台时如何保持 GPS 接收器位置更新(Google Play 服务位置 API)