android marshmallow - SMS_RECEIVED permission

前端 未结 5 1376
粉色の甜心
粉色の甜心 2020-12-20 01:51

i recently updated my app to support android 6 marshmallow. i followed the instruction on https://developer.android.com/training/permissions/requesting.html

and add

5条回答
  •  余生分开走
    2020-12-20 02:30

    You need a permission for api level 23+, google reworked the permission system so the app user can grant and revoke permissions after installing your app

    final private int REQUEST_CODE_ASK_PERMISSIONS = 123;       
    
      if(Build.VERSION.SDK_INT < 23){
         //your code here
       }else {
        requestContactPermission();
       } 
    
    private void requestContactPermission() {
    
       int hasContactPermission =ActivityCompat.checkSelfPermission(context,Manifest.permission.RECEIVE_SMS);
    
       if(hasContactPermission != PackageManager.PERMISSION_GRANTED ) {
        ActivityCompat.requestPermissions(Context, new String[]   {Manifest.permission.RECEIVE_SMS}, PERMISSION_REQUEST_CODE);
       }else {
        //Toast.makeText(AddContactsActivity.this, "Contact Permission is already granted", Toast.LENGTH_LONG).show();
         }
    }
    
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String[]           permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_PERMISSIONS:
            // Check if the only required permission has been granted
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i("Permission", "Contact permission has now been granted. Showing result.");
                Toast.makeText(this,"Contact Permission is Granted",Toast.LENGTH_SHORT).show();
            } else {
                Log.i("Permission", "Contact permission was NOT granted.");
             } 
             break;
      }
    }
    

提交回复
热议问题