-1

私はSMSアプリケーションを持っています。 今私はlink hereから得たこのコードを持っています。アクセス許可が付与されているかどうかをアプリにチェックさせる方法(6、x +)

int MY_PERMISSIONS_REQUEST_READ_CONTACTS=0; 
 
// Here, thisActivity is the current activity 
 
     if (ContextCompat.checkSelfPermission(this, 
 
       Manifest.permission.SEND_SMS) 
 
       != PackageManager.PERMISSION_GRANTED) { 
 

 
      // Should we show an explanation? 
 
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
 
        Manifest.permission.SEND_SMS)) { 
 

 
       // Show an expanation to the user *asynchronously* -- don't block 
 
       // this thread waiting for the user's response! After the user 
 
       // sees the explanation, try again to request the permission. 
 

 
      } else { 
 

 
       // No explanation needed, we can request the permission. 
 

 
       ActivityCompat.requestPermissions(this, 
 
         new String[]{Manifest.permission.SEND_SMS}, 
 
         MY_PERMISSIONS_REQUEST_READ_CONTACTS); 
 

 
       // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
 
       // app-defined int constant. The callback method gets the 
 
       // result of the request. 
 
      } 
 
     }

しかし、私はアセスを拒否(ユーザーが誤ってボタンを押した場合)何もかもはかなり(原因SMSボタンが仕事を文句を言わない)アプリのクラッシュを行います。だから、ユーザがアクセスを拒否した後に、ユーザがアプリケーションを起動/再起動したとき、私はアプリに再び尋ねることを望みます。私のエミュレータを試してみたら、ダイアログが現れず、設定/アプリケーションにアクセスしてパーミッションを設定しなければなりませんでした。または、アンインストールして再度インストールしてください。

+0

可能な複製(http://stackoverflow.com/questions [Androidのマシュマロ要求許可?]/33666071/android-marshmallow-request-permission) – Rohan

答えて

0

あなたが現在行っていることは、ユーザーに許可を求めることですが、アクセス許可が実際に付与されているかどうかを確認することはありません。アクセス許可を要求するアクティビティにonRequestPermissionsResultメソッドを実装することにより、これを行うことができます。

詳細については、https://stackoverflow.com/a/34722591/3872500を参照してください。

変更を加える必要があるコメントでの作業のアプリとは別の許可を使用して
0

例:の

if (ContextCompat.checkSelfPermission(this, 
     Manifest.permission.ACCESS_COARSE_LOCATION) 
     != PackageManager.PERMISSION_GRANTED) { 
    progressLayout.setVisibility(View.GONE); 
    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
      Manifest.permission.ACCESS_COARSE_LOCATION)) { 

     //This is where you need to ask again if they want to allow the permission, 
     //you should show a dialog here to ask if they're sure 
     //and explain why you need the permission. 
     requestDialog = new AlertDialog.Builder(this) 
       .setTitle(getString(R.string.device_setup_location_permission_title)) 
       .setMessage(getString(R.string.device_setup_location_permission_message)) 
       .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         //They clicked yes, they want to enable the permission, 
         //so let's show them the dialog again. 
         ActivityCompat.requestPermissions(DeviceSetupActivity.this, 
           new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 
           REQUEST_LOCATION_RESPONSE); 
        } 
       }) 
       .setNegativeButton(getString(R.string.later), new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         //This means they don't want to grant the permission, 
         //you should exit the app here, perhaps share another dialog saying that in 
         //order to use the app, the permission must be granted. 
         finish(); 
        } 
       }) 
       .show(); 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(this, 
       new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 
       REQUEST_LOCATION_RESPONSE); 

     // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if(requestCode == REQUEST_LOCATION_RESPONSE){ 
     if(hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)){ 
      startScan(); 
     }else{ 
      //exit the activity 
      finish(); 
     } 
    } 
} 

private boolean hasPermission(String perm) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     return(PackageManager.PERMISSION_GRANTED==checkSelfPermission(perm)); 
    } 
    return true; 
} 
関連する問題