使用此代码将用户发送到拨号屏幕非常简单
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);下得到一个波浪线。声明“呼叫需要权限,用户可能会拒绝。代码明确检查权限是否可用”。我如何让它在运行时运行?