Search This Blog

Sunday 21 October 2012

call on a telephone number


This post is describing that how to make a call from the Android phone using Intent.ACTION_CALL.

Start a activity with as follows and pass your telephone number

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + <your tel number>));
activity.startActivity(callIntent);

To check the state of the phone call and actions setup a call listener as follows

private class EndCallListener extends PhoneStateListener { 
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if(TelephonyManager.CALL_STATE_RINGING == state) {
            Log.i("contactus", "RINGING, number: " + incomingNumber);
        }
        if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
            Log.i("contactus", "OFFHOOK");
        }
        if(TelephonyManager.CALL_STATE_IDLE == state) {
            if (incomingNumber.equals(szListentelNumber) == true) {
                 Intent i = activity.getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(
                                activity.getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    activity.startActivity(i);
             }
            Log.i("contactus", "IDLE" + incomingNumber);
        }
    }
}
public void setUpCallListener(String szTelNumber) {
     this.szListentelNumber = szTelNumber;
     EndCallListener callListener = new EndCallListener();
     TelephonyManager mTM = (TelephonyManager)activity.getSystemService(Context.TELEPHONY_SERVICE);
     mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
    
}

Enjoy the coding with Android and if you like this simple post please provide your feedback.

Thanks
Creative Android Apps