2015-11-02 39 views
11

Tôi đang cố gắng để lập trình gọi đến một số với mã sau:phép Revoked android.permission.CALL_PHONE

String number = ("tel:" + numTxt.getText()); 
Intent intent = new Intent(Intent.ACTION_CALL); 
intent.setData(Uri.parse(number)); 
startActivity(intent); 

tôi đã thiết lập sự cho phép trong Manifest:

<uses-permission android:name="android.permission.CALL_PHONE"/> 

tôi m làm việc với thiết bị thực để thử nghiệm và gỡ lỗi, đó là Nexus 5 với Android M, compileSdkVersion của tôi là 23. Tôi nhận được ngoại lệ bảo mật sau:

error: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{cbbd7c1 5228:com.dialerTest.DialerApp/u0a96} (pid=5228, uid=10096) with revoked permission android.permission.CALL_PHONE 

Tôi đã tìm kiếm trên web và cộng đồng này cho Q/A tương tự và không thể tìm thấy câu trả lời. Bất kỳ trợ giúp sẽ được đánh giá cao.

Trả lời

13

Trong android 6.0 (Api lvl 23), chúng tôi có một cái gì đó gọi là "Thời gian chạy quyền". Bạn phải đọc về nó.

Bạn có thể tìm tài liệu here.

+0

Cảm ơn bạn, Artur, tôi đã thành công để giải quyết vấn đề này với hướng dẫn tài liệu. Tôi hiểu rằng trên Android <23 sự cho phép vẫn sẽ được yêu cầu cài đặt, tôi có đúng không? –

+0

@LuciusHipan Có, cho tính tương thích ngược. –

7

Giấy phép CALL_PHONE thuộc nhóm quyền nguy hiểm.
Vì vậy, nếu ứng dụng của bạn nhắm mục tiêu SDK từ 23 trở lên và thiết bị của bạn đang chạy trên Android 6.0 trở lên, bạn phải yêu cầu quyền CALL_PHONE trong khi ứng dụng đang chạy.

Ví dụ:

String number = ("tel:" + numTxt.getText()); 
mIntent = new Intent(Intent.ACTION_CALL); 
mIntent.setData(Uri.parse(number)); 
// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, 
      Manifest.permission.CALL_PHONE) 
    != PackageManager.PERMISSION_GRANTED) { 

    ActivityCompat.requestPermissions(thisActivity, 
      new String[]{Manifest.permission.CALL_PHONE}, 
      MY_PERMISSIONS_REQUEST_CALL_PHONE); 

    // MY_PERMISSIONS_REQUEST_CALL_PHONE is an 
    // app-defined int constant. The callback method gets the 
    // result of the request. 
} else { 
    //You already have permission 
    try { 
     startActivity(mIntent); 
    } catch(SecurityException e) { 
     e.printStackTrace(); 
    } 
} 

Khi yêu cầu ứng dụng cho phép, hệ thống trình bày một hộp thoại cho người dùng. Khi người dùng phản hồi, hệ thống sẽ gọi phương thức onRequestPermissionsResult() của ứng dụng của bạn, chuyển cho nó phản hồi của người dùng.

@Override 
public void onRequestPermissionsResult(int requestCode, 
    String permissions[], int[] grantResults) { 
    switch (requestCode) { 
    case MY_PERMISSIONS_REQUEST_CALL_PHONE: { 
     // If request is cancelled, the result arrays are empty. 
     if (grantResults.length > 0 
      && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

      // permission was granted, yay! Do the phone call 

     } else { 

      // permission denied, boo! Disable the 
      // functionality that depends on this permission. 
     } 
     return; 
    } 

    // other 'case' lines to check for other 
    // permissions this app might request 
    } 
}