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
是的,但我仍然不明白。请帮忙 –