Activity类:
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class PhoneActivity extends Activity {
private EditText phoneText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
phoneText=(EditText)findViewById(R.id.phoneText);
call();
}
public void call(){
Button phonebutton=(Button)findViewById(R.id.button);
phonebutton.setOnClickListener(new CallOnClick());
}
private final class CallOnClick implements OnClickListener{
public void onClick(View v) {
String phonenumber=phoneText.getText().toString();
if(phonenumber==null||phonenumber.length()<1){
Toast.makeText(PhoneActivity.this,R.string.empty, Toast.LENGTH_SHORT).show();
}else{
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenumber));
startActivity(intent);
Toast.makeText(PhoneActivity.this,R.string.success, Toast.LENGTH_SHORT).show();
}
}
}
}
Manifest文件中,配置电话拨号的permission
<uses-permission android:name="android.permission.CALL_PHONE"/>