Android:首次打开应用程序时未加载文件

时间:2016-11-08 11:05:47

标签: android

我编写了一个Android应用程序,它将读取主要活动的onCreate中的txt文件。它工作正常。但是我发现在Android 6上,当我第一次打开应用程序时,它要求我允许从存储中读取文件的权限。因此,应用程序无法第一次读取文件,因为需要等待用户的操作。

如何在用户授予权限后立即读取文件?

我要求获得Android 6的许可,如下所示:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MainActivity.REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);
            }

            if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MainActivity.REQUEST_PERMISSION_READ_EXTERNAL_STORAGE);
            }
        } 

谢谢。

2 个答案:

答案 0 :(得分:1)

我在另一个案例中遇到了这个问题,以获取用户的位置,但我无法重启。

我像打击一样处理它。问题是,当您在打开活动或片段时请求onCreated方法的权限时,它首先创建视图,然后请求权限,以便当您允许应用程序访问权限请求时代码已经执行,因此它将无法工作直到下次重新启动但是android用onRequestPermissionsResult方法解决了这个问题,它将等待用户决定是否允许请求,然后它将执行该方法。在onActivityCreated内移动您的权限检查:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) // grant the access from user when the activity created
            != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, // if the permission wasn't granted so ask for permission
                PERMISSION_ACCESS_FINE_LOCATION);
    } else { // if it was granted so get the location
        getLocation();
    }
}

然后你需要在onRequestPermissionsResult方法中立即执行这些操作:

 @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        boolean allowed = true;
        switch (requestCode) {
            case PERMISSION_ACCESS_FINE_LOCATION:
                // If request is cancelled, the result arrays are empty.
                for (int res : grantResults) {
                    allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
                }
                break;
            default:
                allowed = false;
                break;
        }
        if (allowed) {
            getLocation();
        } else {
            Toast.makeText(getContext(),"You need to 'Enable' the location service", Toast.LENGTH_SHORT).show();
        }
}

在这种情况下你不需要重新启动你的应用程序它会在我第一次这样做的时候工作并完全希望它可以帮助你

答案 1 :(得分:0)

您应该从onRequestPermissionsResult()

获得回复
@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
switch (requestCode) {
    case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! Do the
            // contacts-related task you need to do.

        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
        return;
    }

    // other 'case' lines to check for other
    // permissions this app might request
}
}

您可以阅读更多here