Android Preview M:在授予权限后重新创建活动 [英] Android Preview M: activity recreates after permission grant

查看:92
本文介绍了Android Preview M:在授予权限后重新创建活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试用Preview M并在其上测试我的应用程序,尤其是将文件保存到外部存储"部分. 在下载/保存过程开始之前,我要求

I play around with the Preview M and test my app on it, especially the "saving a file to external storage" part. Before the download/save process starts, i request for

  Manifest.permission.WRITE_EXTERNAL_STORAGE

开发者页面上所述的权限: https://developer.android.com/preview/features /runtime-permissions.html

permission as described on the developer page: https://developer.android.com/preview/features/runtime-permissions.html

该对话框按预期发生:

"Allow <AppName> to access photos, media, and files on your device?" Deny / Allow

如果我点击拒绝"按钮,则会调用该活动的onRequestPermissionsResult方法.

If i hit the "deny"-button, the onRequestPermissionsResult-method of the activity is called.

如果我按下允许"按钮,则将首先重新创建活动,然后再调用onRequestPermissionsResult方法.我认为这是授予许可的结果.

If i press the "allow"-button, the activity is first recreated and afterwards the onRequestPermissionsResult-method is called. I think it's a result of the granted permission.

但是第二种情况很棘手,因为我想触发一个开始下载的回调,但是此对象此时为null:

But the second situation is tricky because i want to trigger a callback that starts the download, but this object is null at this point:

public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {

    getLogger().error("onRequestPermissionsResult ( " + requestCode + ", " + permissions + ", " + grantResults + " )");
    switch (requestCode) {


        //permission for saving files?
        case PermissionCode.WRITE_EXTERNAL_STORAGE: {


            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                //HERE IS THE NULL-OBJECT 
                if (controller != null) {

                    controller.triggerCallback();
                }
            }
            break;
        }
        default: {

            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

所以我的问题是:

  1. 我可以避免这种娱乐吗?
  2. 如果没有,我如何重新设计我的代码以解决问题-目前我还是一无所知

我尝试使用处理程序和postDelayed解决问题-但我跳过了它.

I tried to solve the problem with a handler and postDelayed - but i skipped it.

我初步解决了该问题,并向用户展示了Toast,并请求再次按下下载按钮(在获得许可的情况下).但是Google:您认真吗?

I preliminary solved it and show a Toast to user with the request to push the download button again (in case of granted permission). But Google: ARE YOU SERIOUS?

使用最新的SDK 6.0(版本23)不会发生娱乐活动-也许有人听到我在Mountain View中哭泣:-)

No recreation happens with the latest SDK 6.0 (Version 23) - maybe somebody heard my weeping in Mountain View :-)

推荐答案

在活动在onCreate()中重新创建自身之后,您始终可以立即检查下载条件:

You can always check the download condition immediately after the activity recreates itself in onCreate():

static final int MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
boolean initiateDownload = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState != null) {
        initiateDownload = savedInstanceState.getBoolean("toDownload");
    }
}

@Override
public void onResume() {
    super.onResume();
    final boolean hasPermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED;
    if(initiateDownload && hasPermission) {
        // start download here...
    } else {
        requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE},
                MainActivity.MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE) {
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            initiateDownload = true;
        } else {
            // denied permission...
        }
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("toDownload", initiateDownload);
}

这篇关于Android Preview M:在授予权限后重新创建活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆