2016-05-16 86 views
1

如何申請Permisson?我嘗試了dcumentation,但是常量int請求代碼MY_PERMISSIONS_REQUEST_CALL_PHONE donst似乎只是工作,爲了向後兼容性還需要記住其他任何事情?Android Permisson請求代碼問題

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

如何聲明MY_PERMISSIONS_REQUEST_CALL_PHONE常量int?

+0

也許這將是有用的爲你http://stackoverflow.com/a/36787464/3436179 – Alexander

+0

檢查這個裁判:http://www.tutorialspoint.com/android/android_phone_calls.htm –

回答

4

對於較低的版本,則需要在清單隻, 申報權限,但對於MARSHMELLOW你需要給它的代碼,您要執行的代碼。

在這裏,你想打個電話。因此,在編寫的代碼塊中插入/包含下面提供的代碼以進行呼叫。

public void makeCall() 
    { 
     Intent intent = new Intent(Intent.ACTION_CALL); 
     intent.setData(Uri.parse("tel:" + "123456")); 
     int result = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE); 
     if (result == PackageManager.PERMISSION_GRANTED){ 

      startActivity(intent); 

     } else { 

      requestForCallPermission(); 
     } 
    } 

    private void requestPermission() 
    { 

     if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CALL_PHONE)) 
     { 
     } 
     else { 

      ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE); 
     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) 
    { 
    switch (requestCode) { 
     case PERMISSION_REQUEST_CODE: 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       makeCall(); 
      } 
      break; 
     } 
    } 
+0

是的,我明白,但如何delcare PERMISSION_REQUEST_CODE – ueen

+1

對不起,我忘了通知你,你需要在下面的頂部聲明它像private static final int PERMISSION_REQUEST_CODE = 1; – Vickyexpert

+0

還可以通過CALL_PHONE而不是ACCESS_FINE_LOCATION在else部分更改requestPermission()方法中的權限 – Vickyexpert

0
  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // ActivityCompat#requestPermissions 
       // here to request the missing permissions, and then overriding 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
       //           int[] grantResults) 
       // to handle the case where the user grants the permission. See the documentation 
       // for ActivityCompat#requestPermissions for more details. 
       return; 
      } 
Intent intent = new Intent(Intent.ACTION_CALL); 
      intent.setData(Uri.parse("tel:" + "123456")); 
      startActivity(intent); 

試着做這個。

+0

感謝它開始工作,我想我把如果之前的startActivity :) – ueen

+0

是的,這是唯一的問題。保持學習。 :) –

0

嘗試下面的代碼希望它會幫助你。 首先,在允許撥打電話號碼後,會要求您提供允許彈出窗口。

if (ContextCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale(HomePanelActivity.this, 
        Manifest.permission.CALL_PHONE)) { 
       ActivityCompat.requestPermissions(HomePanelActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PERMISSION); 
      } 
     } else { 
      Intent callIntent = new Intent(Intent.ACTION_CALL); 
      callIntent.setData(Uri.parse("tel:" + phoneNumber)); 
      if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { 
       startActivity(callIntent); 
      } 
     } 

@Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case 10: 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        Intent callIntent = new Intent(Intent.ACTION_CALL); 
        callIntent.setData(Uri.parse("tel:" + phoneNumberToCall)); 
        if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { 
         startActivity(callIntent); 
        } 
       } else { 
        Snackbar.make(drawerLayout, "You Deny permission", Snackbar.LENGTH_SHORT).show(); 
       return; 
      } 
     } 
    }; 
+0

我需要在啓動時使用Permisson,並在點擊按鈕時進行調用。 而我需要的權限,所以否認是沒有選擇... – ueen

+0

這個請求者API23我的應用程序是minSDK 15 – ueen

+0

是的,你需要編譯SDK版本爲23.最低15將工作。 –

6
public void makeCall(String s) 
{ 
    Intent intent = new Intent(Intent.ACTION_CALL); 
    intent.setData(Uri.parse("tel:" + s)); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){ 

     requestForCallPermission(); 

    } else { 
     startActivity(intent); 

    } 
} 
public void requestForCallPermission() 
{ 

    if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CALL_PHONE)) 
    { 
    } 
    else { 

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

@Override 
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) 
{ 
    switch (requestCode) { 
     case PERMISSION_REQUEST_CODE: 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       makeCall("100"); 
      } 
      break; 
    } 
} 

//現在調用方法MakeCall函數( 「your_desire_phone_numder」); makeCall(「100」); Link for more details