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

Can't click Allow External Write Access Permissions in Android

当 twilight(屏幕变暗)等其他后台应用程序在后台运行时,我无法允许该应用程序写入外部存储。在这种情况下,我只能否认它并说检测到屏幕覆盖。但是不能让。

我的代码:

    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();
            }
        }
    }

}

我想要它 运行 即使像 twilight 这样的后台应用是 运行。

使用 Android 的内置下载管理器下载特定文件可提供完整性和更加用户友好的方法。如果还支持具有 Marshmallow 及更高版本的设备,则还应在执行此操作之前调用 requestPermission 方法。

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

public void downloadFile(String uRl) {
    File direct = new File(Environment.getExternalStorageDirectory()
            + "/" + "MyFolder");

    if (!direct.exists()) {
        direct.mkdirs();
    }

    DownloadManager mgr = (DownloadManager) getSystemService(this.DOWNLOAD_SERVICE);

    Uri downloadUri = Uri.parse(uRl);
    DownloadManager.Request request = new DownloadManager.Request(downloadUri);

    request.setAllowedNetworkTypes(
            DownloadManager.Request.NETWORK_WIFI
                    | DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false)
            .setTitle("AppNameAsTitle")
            .setDescription("Downloaded using My app")
            .setDestinationInExternalPublicDir("/MyFolder", "filename.jpg")
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    mgr.enqueue(request);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

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

    } else {
        Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
    }
}