package com.example.phonecall; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn=(Button)findViewById(R.id.Call); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub makeCall(); } private void makeCall() { // TODO Auto-generated method stub Log.i("Make call", ""); Intent phoneIntent = new Intent(Intent.ACTION_CALL); phoneIntent.setData(Uri.parse("tel:10086")); try { startActivity(phoneIntent); finish(); Log.i("Finished making a call...", ""); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(MainActivity.this, "Call faild, please try again later.", Toast.LENGTH_SHORT).show(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/Call" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/make_call"/> </LinearLayout>
Strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Phonecall</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="make_call">Call 10086</string> </resources>
AndroidManifest.xml中写入权限:
<uses-permission android:name="android.permission.CALL_PHONE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
run: