Recognize incoming PhoneCalls

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Recognize incoming PhoneCalls

Postby plusminus » Wed Nov 28, 2007 4:42 pm

Recognize incoming PhoneCalls :!:


What you will learn: You will learn how to recognize on incoming PhoneCalls by using a PhoneStateIntentReceiver. Possible scenario is, to disable your music-player, when there is an incoming Phone-Call.

:?: Problems/Questions: Write it right below...

Difficulty: 2 of 5 :)

What it will look like:
Unfortunately nothing to see here :cry:


Description:
We will create an PhoneStateIntentReceiver that sends Messages to a Handler when the PhoneState changes. Take a look at the heavily commented example-implementation (The actual registering takes place in the onCreate()-method):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.reactonincomingcall;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.telephony.Phone;
  8. import android.telephony.PhoneStateIntentReceiver;
  9. import android.util.Log;
  10.  
  11. public class ReactOnIncomingCall extends Activity {
  12.         /** Used to recognize Messages from the
  13.          * myPhoneStateChangedHandler. */
  14.         final int PHONECALLSTATE_RECONGNIZE_ID = 0x539;
  15.        
  16.         /** Will notify us on changes to the PhoneState*/
  17.         PhoneStateIntentReceiver myPsir = null;
  18.  
  19.     /** This Handler will react on the messages the
  20.      * we made our PhoneStateIntentReceiver myPsir
  21.      * notify us on. */
  22.     Handler myPhoneStateChangedHandler = new Handler(){
  23.  
  24.                 @Override
  25.                 public void handleMessage(Message msg) {
  26.  
  27.                         // Recognize the Message by its what-ID
  28.                         if(msg.what == PHONECALLSTATE_RECONGNIZE_ID){
  29.  
  30.                                 /* Our PhoneStateIntentReceiver myPsir
  31.                                  * now contains some recent data, we can grab. */
  32.                                 Phone.State myState = myPsir.getPhoneState();
  33.  
  34.                                 // Put the Info to the logger for debugging
  35.                                 Log.d("PhoneCallStateNotified", myState.toString());
  36.  
  37.                                 if(myState == Phone.State.RINGING){
  38.                                         // Celebrate =D
  39.                                 }                      
  40.                         }
  41.                 }
  42.     };
  43.        
  44.     /** Called when the activity is first created. */
  45.     @Override
  46.     public void onCreate(Bundle icicle) {
  47.         // Set some simple layout
  48.         super.onCreate(icicle);
  49.         setContentView(R.layout.main);
  50.  
  51.        
  52.         /* Create a new PhoneStateIntentReceiver
  53.          * that will pass messages to the handler h
  54.          * as it receives Intents we make it notify
  55.          * us below*/
  56.         this.myPsir = new PhoneStateIntentReceiver(this, myPhoneStateChangedHandler);
  57.        
  58.         /* As we want to get notified on changes
  59.          * to the Phones-State we tell our
  60.          * PhoneStateIntentReceiver myPsir,
  61.          * that we wan to get notified with the ID
  62.          * (PHONECALLSTATE_RECONGNIZE_ID) we pass to him
  63.          */
  64.         this.myPsir.notifyPhoneCallState(PHONECALLSTATE_RECONGNIZE_ID);
  65.                
  66.                 /* Register the Intent with the system. */
  67.         this.myPsir.registerIntent();
  68.     }
  69. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby bibiodp » Wed Nov 28, 2007 4:53 pm

THX A LOT :) :)
I'm a poor lonesome French developper ;)
bibiodp
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Nov 26, 2007 6:18 pm

Postby königdmx » Mon Feb 25, 2008 9:59 am

@plusminus:

From where you have the value 0x539?? And what defined it?
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final int PHONECALLSTATE_RECONGNIZE_ID = 0x539;
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Greetz
königdmx
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Feb 19, 2008 11:09 am

Postby plusminus » Mon Feb 25, 2008 10:24 am

:D

its a Hex number. Convert it to Decimal (i.e. with win-calc) and you know why I chose this number ^^

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby königdmx » Mon Feb 25, 2008 10:54 am

:D
ok, hex is clear!!

but, 1337?
I dont know what you mean!
Why 1337?

Ok, I checked this.
But then I dont check the functionality of this example.
königdmx
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Feb 19, 2008 11:09 am

Postby parashar.iiit » Thu Apr 03, 2008 8:43 pm

Hello plusminus ,
Thanks for this application. How can we reject a call through code ...or transfer to automatic answering machine ...like playing a mp3 for the caller ???


Thanks
Abhinav
User avatar
parashar.iiit
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sat Feb 02, 2008 12:51 pm

Top

Postby plusminus » Thu Apr 10, 2008 11:43 pm

I've never digged into that issue, but as far as I know Call-Controlling is not possible. ?!!??

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby legerb » Sun Oct 05, 2008 2:55 pm

Is it possible to catch the moment when the call is accepted (both outgoing and incoming)?
legerb
Freshman
Freshman
 
Posts: 4
Joined: Thu Sep 25, 2008 10:48 am

Postby mrobinton » Thu Oct 16, 2008 8:04 pm

Is this tutorial applicable to SDK 1.0?
mrobinton
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Oct 16, 2008 7:59 pm

Postby bino » Mon Oct 20, 2008 5:40 am

mrobinton wrote:Is this tutorial applicable to SDK 1.0?


In SDK 1.0, you should use PhoneStateListener and TelephonyManager. You can refer to this link:
http://groups.google.com/group/android- ... 9a3708cbe3

It will help!

:)
User avatar
bino
Freshman
Freshman
 
Posts: 9
Joined: Mon Oct 13, 2008 7:15 am

Postby tabrej.khan » Sat Dec 06, 2008 1:14 pm

Hi PlusMinus,

I am successfully able to run my application in the background and start it when any call comes...

I have started an activity when a call comes...

I would like to know how can I make my activity in front instead of going back when a call comes?
User avatar
tabrej.khan
Freshman
Freshman
 
Posts: 8
Joined: Wed Jul 16, 2008 6:16 am
Location: Bangalore, Karnataka

Problems

Postby jankware » Sat Jan 03, 2009 1:09 am

I'm trying to create this myself however I'm coming up with errors that:

Imports android.telephony.phone;
Imports android.telephony.PhoneStateIntentReveiver;

are not valid? is this due to new versions of the SDK?

if so what should we use in place of these?

Thanks,
Keith
Keith
jankware
Freshman
Freshman
 
Posts: 3
Joined: Tue Dec 23, 2008 8:44 am
Location: San Jose

Postby abraxas » Sun Mar 15, 2009 11:39 pm

Even this is an older thread, somebody might want to know the change of getting informed by call state changes.

Since SDK 1.0 you have to use a PhoneStateListener.
Extend it and overwrite the callback method you want to get informed about.

Code: Select all
class MyPhoneStateListener extends PhoneStateListener {

      @Override
      public void onCallStateChanged(int state, String incomingNumber) {
         super.onCallStateChanged(state, incomingNumber);
         
         switch (state){
         case TelephonyManager.CALL_STATE_RINGING:
            //Log.d(TAG, "ringing");
            break;
         
         case TelephonyManager.CALL_STATE_IDLE:
            Log.d(TAG, "idle");
            break;

         case TelephonyManager.CALL_STATE_OFFHOOK :
            Log.d(TAG, "offhook");
            break;

         }
      }

Don't forget to tell the TelephonyManager to register your PhoneStateListener, passing the event types you are interested in.

Code: Select all
((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE))
         .listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
abraxas
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Feb 06, 2009 12:33 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests