Thursday 28 February 2013

Making Phone Call Example in Android

main.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <EditText
            android:id="@+id/number_edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="35dp"
            android:layout_marginTop="16dp"
            android:ems="10"
            android:inputType="phone" />

        <Button
            android:id="@+id/call_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/number_edit_text"
            android:layout_marginLeft="72dp"
            android:layout_marginTop="36dp"
            android:text="Make Call" />

    </RelativeLayout>

 MainActivity.java

 package com.rajeshvijayakumar.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    private Button mCallbutton;
    private EditText mNumberEditText;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mNumberEditText = (EditText) findViewById(R.id.number_edit_text);
        mCallbutton = (Button) findViewById(R.id.call_button);

        PhoneCallListener phoneListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);

        // add button listener
        mCallbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+mNumberEditText.getText().toString()));
                startActivity(callIntent);

            }

        });

    }

    private class PhoneCallListener extends PhoneStateListener {

        private boolean isPhoneCalling = false;
        private String TAG = PhoneCallListener.class.getSimpleName();
       
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (TelephonyManager.CALL_STATE_RINGING == state) {
                // phone ringing
                Log.d(TAG, "RINGING STATE");
            }

            if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                // active
                Log.d(TAG, "OFFHOOK STATE");
                isPhoneCalling = true;
            }

            if (TelephonyManager.CALL_STATE_IDLE == state) {
                Log.d(TAG, "IDLE");
            }
        }
    }
}

 Output :








Source Code : Download this Example Here









2 comments:

  1. can we get default call keypad means when we call some one we get phone num keypad, i want that key pad in my application can we set?

    ReplyDelete
    Replies
    1. use inputType attribute in your Editext tag and you can set different keypad

      Delete