main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnLaunchActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨打号码" />
<EditText
android:id="@+id/et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Activit.java
package com.example.openweb;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.btnLaunchActivity);
et= (EditText)findViewById(R.id.et);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Uri uri = Uri.parse("tel:"+et.getText());
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
}
});
}
}
如果需直接拨打的话:
Uri uri = Uri.parse("tel:"+et.getText());
Intent it = new Intent(Intent.ACTION_CALL, uri);
startActivity(it);
如果报错的话:E/AndroidRuntime(739): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxx-xxx-xxxx cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{406c0a38 739:com.example.openweb/10042} (pid=739, uid=10042) requires android.permission.CALL_PHONE
需增加:
<uses-permission android:name="android.permission.CALL_PHONE"/>