當我從edittext獲取號碼時,如何撥打電話?我嘗試這個,但它不起作用。我有一個傳遞變量的問題到Uri.parse()必須在無效的gettext?撥打android並從edittext框中獲取號碼
public String string;
public String number;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText txtcallnumber;
txtcallnumber = (EditText)findViewById(R.id.callnumber);
string = txtcallnumber.getText().toString().trim();//There no work call
number = "tel:" + string;
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
call();
}
});
}
public void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
//callIntent.setData(Uri.parse("tel:xxxxxxx")); //This work
string = txtcallnumber.getText().toString().trim();
number = "tel:" + string;//There work call
callIntent.setData(Uri.parse(number));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("helloandroid dialing example", "Call failed");
}
好工作now.Thanks.But爲什麼工作的時候裏面是空? – user1909897
@ user1909897什麼意思是「inside void?」 –
@ user1909897你的方式絕對正確,但ACTION_CALL需要權限,這就是爲什麼你的日誌中出現安全錯誤。 –