2016-07-21 82 views
0

我需要從我的應用程序調用給定的電話號碼。Android請求權限不會顯示對話框

所以我必須驗證通話權限。

但對話框顯示在我的應用程序中,直接它是拒絕功能。我在這裏做了什麼問題。請查看我的代碼並給我一個解決方案。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
       ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 105); 
      } else { 
       startActivity(callIntent); 
      } 
     } else { 
      startActivity(callIntent); 
     } 

@Override 
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case 105: 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       callJobseeker(); 
      } else { 
       Toast.makeText(getApplicationContext(), "You cannot make a call.Because call permission is denied", Toast.LENGTH_SHORT).show(); 
      } 
      break; 
       } 
       } 
清單文件

我給喜歡權限:

<uses-permission android:name="android.permission.CALL_PHONE" /> 
<permission android:name="com.example.permission.CALL_PHONE" 
    /> 
+0

在哪個android os中檢查? – NilayDani

+0

@NilayDani正在檢查6.0版本 –

+0

@KrishnaVeni爲什麼你有寫權限com.jobsearchtry.permission.CALL_PHONE –

回答

0

嘗試shouldShowRequestPermissionRationale

//Function use for request permission to access camera & gallary. 
    private void requestPermission() { 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) { 
      Toast.show(context, getString(R.string.requestPermission)); 
     } else { 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CODE); 
     } 
    } 
0

你可以這樣做。讓類像下面

public class CheckPermisson { 

    private Context context; 

    public CheckPermisson(Context context) { 
     this.context = context; 
    } 

    public boolean checkCallPermission() { 
     if (ContextCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { 
      Log.i("TAG","Call Permission is granted"); 
      return true; 


     } else { 

      return false; 
     } 

    } 
    public boolean checkExternalStoragePermission() { 
     if (ContextCompat.checkSelfPermission(context.getApplicationContext(),android.Manifest.permission.WRITE_EXTERNAL_STORAGE) 
       == PackageManager.PERMISSION_GRANTED) { 
      Log.i("TAG", "Storage Permission is granted"); 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
    public boolean checkLocationPermission() { 
     if (ContextCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      Log.i("TAG", "Location Permission is granted"); 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
} 

您可以使用如下

CheckPermisson permisson = new CheckPermisson(getActivity()); 

if (Build.VERSION.SDK_INT >= 23) { 
    if (permisson.checkCallPermission()) { 
     itemSelectedPosition = position; 
     createDialogBox(position); 
    } else { 
     requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, CALL_PERMISSION_REQUEST_CODE); 
    } 
} else { 
    createDialogBox(position); 
} 

清單中

下面添加權限
<uses-permission android:name="android.permission.CALL_PHONE" /> 
0

如果用戶已經授予CALL_PHONE的許可,您應該檢查使用:

if (checkSelfPermission(android.Manifest.permission. CALL_PHONE) 
      == PackageManager.PERMISSION_GRANTED) { 
     Log.v(TAG,"Permission is granted"); 
     return true; 
} 
0

我得到了答案,

我在後的錯誤是,

在Android清單文件,我已刪除了這條線,

<permission android:name="com.example.permission.CALL_PHONE" 
    /> 

我已經改變了以下代碼。,

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CALL_PHONE}, 105); 
       } else { 
        startActivity(callIntent); 
       } 
相關問題