Friday 2 November 2012

Call number on button click PhoneStateListener

Call number on Button click in Android and Use of Phone State Listener in Android

     This is a simple demo tutorials that will help you to understand how to call a phone number on button click.

     An Android allows you to call any phone number from your application.

For that you need to call and android intent and have to pass phone number with keyword tel: .
To get more updates about this blog click on Join This Site-->
You have to pass argument in particular format as describe below:
format: 
tel:phonenumber
Here is an example code to call a number 
Intent callIntent = new Intent(Intent.ACTION_CALL);

callIntent.setData(Uri.parse("tel:1234567890"));
startActivity(callIntent);

You need to add following permission in AndroidManifest.xml

<android:name="android.permission.CALL_PHONE" />
If you like this tutorial then like us on facebook ----->
PhoneStateListener

  • A Listener class that will monitor phone state, message waiting.
  • You need to override onCallStateChanged() method
  • Following are list of phone state
    • CALL_STATE_IDLE
    • CALL_STATE_RINGING
    • CALL_STATE_OFFHOOK
       
  • A method onCallStateChanged() will call every time when ever your device phone call state will changed. 
 
Here is example code for how to use PhoneStateListener


private class PhoneCallListener extends PhoneStateListener {
     String LOG_TAG = "GTUMCAlog: ";
     @Override
     public void onCallStateChanged(int state, String incomingNumber) {
            if (TelephonyManager.CALL_STATE_RINGING == state) {    
              
// phone ringing
               Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);

         }
         if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
              // active
              Log.i(LOG_TAG, "OFFHOOK");
              isPhoneCalling = true;
             }
            if (TelephonyManager.CALL_STATE_IDLE == state) {

              Log.i(LOG_TAG, "IDLE");

                  if (isPhoneCalling) {
                       Log.i(LOG_TAG, "restart app");
                  }
        }

      }

}
GTU MCA Android Tutorial How to Call phone number from android on button click.    

Posted by:  Bhavika Jogi
Related Links
  1. Custom intermediate Progress bar demo in android. 
  2. Capture image using camera, save to sd-card and display. 
  3. Latest IT - news updates. 
  4. Free tips and tricks - Android tricks 
  5. Download free projects with Source code. 
  6. List of world celebration days.  

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Bluehost Review