打电话给android并从edittext框中获取号码

时间:2012-12-29 14:50:55

标签: android phone-call

当我从edittext获取号码时,如何拨打电话?我试试这个,但它不起作用。我有一个问题,将变量号传递给Uri.parse()必须在void gettext中?

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");
}

2 个答案:

答案 0 :(得分:0)

您需要在清单文件中添加Permission ::

<uses-permission android:name="android.permission.CALL_PHONE">

在你的call()方法中尝试使用这些Intent ::

number=edittext.getText().trim();
no="tel:"+number;

startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(no)));

答案 1 :(得分:0)

在onCreate方法中

不需要:

 string = txtcallnumber.getText().toString().trim();
 number = "tel:" + string;

因为onCreate几乎用于设置显示,并且在设置屏幕之前用户不会输入,但在输入文本并按下按钮之后在onCall方法中它会...你知道。

另外,您应该检查是否输入了任何内容:

if(string!=null){
  try{try {
Intent callIntent = new Intent(Intent.ACTION_CALL);

string = txtcallnumber.getText().toString().trim();
   number = "tel:" + string;
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(number)));


} catch (ActivityNotFoundException activityException) {
 Log.e("helloandroid dialing example", "Call failed");

      }}else{
    Log.d("helloandroid dialing example","nothing entered");}