2017-04-26 25 views
6

Tôi viết mã để yêu cầu nhiều Run time permission on android 6.0 trong nhóm. tất cả mọi thứ tốt tôi làm theo một số ví dụ tốt cho điều này nhưng vẫn còn có vấn đề.Cho phép nhiều thời gian chạy giấy phép

Trong ActivityCompat.shouldShowRequestPermissionRationale (context, READ_PHONE_STATE) lỗi phát sinh của nó trên ngữ cảnh ngữ cảnh đối số đầu tiên sai. hãy giúp làm thế nào để giải quyết nó.

Cảm ơn trước

Mã là:

if (ContextCompat 
        .checkSelfPermission(SpalshActivity.this, 
          READ_PHONE_STATE)+ContextCompat.checkSelfPermission(context, 
        WRITE_EXTERNAL_STORAGE) +ContextCompat.checkSelfPermission(context, 
        CAMERA) + ContextCompat 
        .checkSelfPermission(context, 
          READ_CONTACTS)+ContextCompat 
        .checkSelfPermission(context, 
          CALL_PHONE)+ContextCompat 
        .checkSelfPermission(context, 
          ACCESS_FINE_LOCATION)+ContextCompat 
        .checkSelfPermission(context, 
          READ_SMS)== PackageManager.PERMISSION_GRANTED) { 
       myMethod(); 

      } 
       else { 
       if (ActivityCompat.shouldShowRequestPermissionRationale 
         (context, READ_PHONE_STATE) ||ActivityCompat.shouldShowRequestPermissionRationale 
         (context, WRITE_EXTERNAL_STORAGE)|| 
         ActivityCompat.shouldShowRequestPermissionRationale 
           (context, CAMERA) || 
         ActivityCompat.shouldShowRequestPermissionRationale 
           (context, READ_CONTACTS) || ActivityCompat.shouldShowRequestPermissionRationale 
         (context, CALL_PHONE) || ActivityCompat.shouldShowRequestPermissionRationale 
         (context, ACCESS_FINE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale 
         (context, READ_SMS)) { 
        Snackbar.make(findViewById(android.R.id.content), 
          "Please Grant Permissions", 
          Snackbar.LENGTH_INDEFINITE).setAction("ENABLE", 
          new View.OnClickListener() { 
           @Override 
           public void onClick(View v) { 
            ActivityCompat.requestPermissions(SpalshActivity.this, 
              new String[]{READ_PHONE_STATE,WRITE_EXTERNAL_STORAGE,CAMERA, READ_CONTACTS, CALL_PHONE, ACCESS_FINE_LOCATION, READ_SMS}, 
              REQUEST_READ_PHONE_STATE); 
           } 
          }).show(); 
       } else { 
        ActivityCompat.requestPermissions(SpalshActivity.this, 
          new String[]{READ_PHONE_STATE,WRITE_EXTERNAL_STORAGE,CAMERA, READ_CONTACTS, CALL_PHONE, ACCESS_FINE_LOCATION, READ_SMS}, 
          REQUEST_READ_PHONE_STATE); 
       } 
      } 
      } 

    } 
+1

Thử yourActivity.this, thay vì ngữ cảnh. –

Trả lời

6

tham số đầu tiên được android.app.Activity loại, Bạn có thể không vượt qua context tại nơi này để sử dụng this thay vì context như dưới đây mã: -

if (ActivityCompat.shouldShowRequestPermissionRationale 
         (this, READ_PHONE_STATE) ||ActivityCompat.shouldShowRequestPermissionRationale 
         (this, WRITE_EXTERNAL_STORAGE)|| 
         ActivityCompat.shouldShowRequestPermissionRationale 
           (this, CAMERA) || 
         ActivityCompat.shouldShowRequestPermissionRationale 
           (this, READ_CONTACTS) || ActivityCompat.shouldShowRequestPermissionRationale 
         (this, CALL_PHONE) || ActivityCompat.shouldShowRequestPermissionRationale 
         (this, ACCESS_FINE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale 
         (this, READ_SMS)) 
0

Thử thay thế context với this

if (ActivityCompat.shouldShowRequestPermissionRationale(this, READ_PHONE_STATE) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, WRITE_EXTERNAL_STORAGE) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, CAMERA) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, READ_CONTACTS) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, CALL_PHONE) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, ACCESS_FINE_LOCATION) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, READ_SMS)) { 
    //... 
} 
Các vấn đề liên quan