无法在Android中单击“允许外部写入访问权限”

时间:2016-12-02 19:05:21

标签: java android android-permissions android-external-storage

当其他后台应用程序(如暮光之城(昏暗屏幕))在后台运行时,我无法允许该应用程序写入外部存储空间。在这种情况下,我只能否认它并且它会检测到屏幕覆盖。但是不能允许。

我的代码:

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

        boolean hasPermission = (ContextCompat.checkSelfPermission(Timetable.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
        if (!hasPermission) {
            ActivityCompat.requestPermissions(Timetable.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    1);
        }
    }

覆盖代码:

 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode)
    {
        case 1: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                //Intent i=new Intent(this,Timetable.class);
                //startActivity(i);
                //reload my activity with permission granted or use the features what required the permission
            } else
            {
                Toast.makeText(Timetable.this, "The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
            }
        }
    }

}

即使像暮光之城这样的后台应用正在运行,我也希望它能够正常运行。

1 个答案:

答案 0 :(得分:1)

使用Android内置的下载管理器下载某个文件可提供完整性和更友好的用户方法。如果还要支持具有Marshmallow及以上版本的设备,则应在调用requestPermission方法之前调用requestPermission方法。

关于允许按钮不起作用,该问题是因为捕获屏幕叠加层的应用或活动。比如Facebook Messenger或任何屏幕调光应用程序。因此,为了使其健壮,您需要在每次下载时检查权限。

String[] array = {"Apple","Banana","Apple","Peanut","Banana","Orange","Apple", "Peanut"};

Map<String, Long> map = Stream.of(array).collect(Collectors.
         groupingBy(Function.identity(), //use groupingBy array element
                 Collectors.counting())); //count number of occurances
System.out.println(map);//output the results of the Map