0

に外部書き込みアクセス権限を許可する]をクリックすることはできません。この場合、私はそれを拒否することしかできず、スクリーンオーバーレイが検出されたと言います。しかし、許可することはできません。私は夕暮れのような他のバックグラウンドのアプリが(画面を暗く)バックグラウンドで実行するときアプリは外部ストレージへの書き込みを許可することはできませんよアンドロイド

マイコード:

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

} 

私は夕暮れのようないくつかのバックグラウンドアプリが実行されている場合でも、それが実行してほしいです。ダウンロードマネージャで作成したAndroidのを使用して特定のファイルをダウンロード

+0

あなたはスクリーンショット/ logcatを追加することはできますか? –

答えて

1

は、整合性と、よりユーザーフレンドリーなアプローチを提供します。またrequestPermission方法は、デバイスは、上記のマシュマロを有する場合も同様にサポートされることを実行する前に呼び出されなければなりません。

そして、許可ボタンが作動しないことについて、その問題は、画面のオーバーレイをキャプチャするアプリケーションや活動です。 Facebookのメッセンジャーや画面の調光アプリなどのように。したがって、堅牢にするには、何かをダウンロードするたびに許可を確認する必要があります。

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

私を助けてくれてありがとうSanVedありがとう!希望は、Androidはすぐに画面のオーバーレイを修正します。ダウンロードマネージャの部分が最適です! – johndoe122

関連する問題