2012-12-29 70 views
1

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

回答

0

您需要在您的清單文件::

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

您的來電()方法中添加內部權限嘗試在onCreate方法使用這些意向::

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

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

好工作now.Thanks.But爲什麼工作的時候裏面是空? – user1909897

+0

@ user1909897什麼意思是「inside void?」 –

+0

@ user1909897你的方式絕對正確,但ACTION_CALL需要權限,這就是爲什麼你的日誌中出現安全錯誤。 –

0

有不需要:

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

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