0
與ACTION_CALL意圖授予發送用戶撥打屏幕相當直截了當使用此代碼權限在運行時
EditText firstNumber;
Button btnAdd;
String hash;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main23);
btnAdd = (Button)findViewById(R.id.button2);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
EditText et = (EditText) findViewById(R.id.editText);
String text= et.getEditableText().toString();
hash = "#";
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:*221*" + text + "hash"));
startActivity(intent);
} catch (Exception e) {
}
}
});
}
然而,當我希望用戶自動進行從一個按鈕呼叫點擊該按鈕應自動化妝一個電話。我已經設置了android.permission.CALL_PHONE,我用這個代碼如下 ....
EditText firstNumber;
EditText secondNumber;
Button btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//assigning where the button5 to btnAdd
btnAdd = (Button)findViewById(R.id.button5);
// set up my setOnClickListener for button
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
//stores whats inside of the editText2 to et and et2
EditText et = (EditText) findViewById(R.id.editText2);
EditText et2 = (EditText) findViewById(R.id.editText5);
//stores whats been sent to et and et2 variables to text and text2
String text= et.getEditableText().toString();
String text2 =et2.getEditableText().toString();
// new intent to take user to dial screen and make the call automatically.
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:*215*" + text + "*" + text2 + "#"));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// crashes here "i get red squigglies here "
startActivity(callIntent);
} catch (Exception e) {
}
}
});
}
我得到一個波浪下startActivity(callIntent);指出「調用需要權限可能會被用戶拒絕。代碼來顯式檢查是否有權限可用」。我如何讓它在運行時運行?
請參閱[在運行時請求權限](https://developer.android.com/training/permissions/requesting.html) – reVerse
是的,但我仍然不明白。請幫忙 –