The most common function of a mobile phone is to call and send text messages, how do we make calls through the program in Android development? This article gives a simple example of making a phone call using an Android phone.
Here are the specific steps to develop this instance:
First, a new Android project, named Phonecalldemo.
Second, design the interface of the program, open main.xml to modify the content as follows:
xml/html Code
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="Vertical"
- Android:layout_width="Fill_parent"
- android:layout_height="Fill_parent"
- >
- <TextView
- Android:layout_width="Fill_parent"
- android:layout_height="Wrap_content"
- android:text="Please input the Phonenumer:"
- />
- <EditText
- android:id="@+id/et1"
- Android:layout_width="Fill_parent"
- android:layout_height="Wrap_content"
- android:phonenumber="true"
- />
- <Button
- android:id="@+id/bt1"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:text="Call Phone"
- />
- </linearlayout>
Third, increase the access to call, open Androidmanifest.xml, modify the code as follows:
xml/html Code
- <? XML version= "1.0" encoding="Utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="Com.android.test"
- android:versioncode="1"
- Android:versionname="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=". Phonecalldemo "
- Android:label="@string/app_name">
- <intent-filter>
- <action android:name="Android.intent.action.MAIN" />
- <category android:name="Android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity> </application>
- <uses-sdk android:minsdkversion="3" />
- <uses-permission android:name="Android.permission.CALL_PHONE">
- </uses-permission>
- </manifest>
Four, the main program Phonecalldemo.java code is as follows:
Java code
- Package com.android.test; import android.app.Activity;
- Import android.content.Intent;
- Import Android.net.Uri;
- Import Android.os.Bundle;
- Import Android.view.View;
- Import Android.widget.Button;
- Import Android.widget.EditText;
- Import Android.widget.Toast;
- public class Phonecalldemo extends Activity {
- Private Button BT;
- Private EditText et;
- Public void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Access to resources
- BT = (Button) Findviewbyid (R.ID.BT1);
- ET = (EditText) Findviewbyid (R.ID.ET1);
- Increase incident Response
- Bt.setonclicklistener (new Button.onclicklistener () { @Override
- Public void OnClick (View v) {
- Get the input phone string
- String inputstr = Et.gettext (). toString ();
- If the input is not empty create a call intent
- if (Inputstr.trim (). Length ()! =0)
- {
- Intent phoneintent = new Intent ("Android.intent.action.CALL",
- Uri.parse ("Tel:" + inputstr));
- Start
- StartActivity (phoneintent);
- }
- Otherwise, Toast prompts
- else{
- Toast.maketext (Phonecalldemo. This, "cannot be entered as empty", Toast.length_long). Show ();
- }
- }
- });
- }