First of all we need to set our Manifest file to listen to the Phone State, to do that we need to edit our it.
<application>
.....
<receiver android:name=".ServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>Here you can see that we created a receiver xml node inside our application and have the java class ServiceReceiver to listen to it. What it would listen to is the PHONE_STATE and thus we need the permission of READ_PHONE_STATE. Then our ServiceReceiver Class would look like this.
public class ServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MyPhoneStateListener phoneListener=new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}
}For the full class including the imports, please download the files below. In here we have another class called MyPhoneStateListener, which would be shown at the bottom. What this class would do is execute the phoneListener when the telephony.listen has received a LISTEN_CALL_STATE.
public class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state,String incomingNumber){
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
break;
}
}
}What we have is a function called onCallStateChanged which would be fired when the LISTEN_CALL_STATE dispatches it. The states are either, ringing(CALL_STATE_RINGING), answers (CALL_STATE_OFFHOOK), or hang up/end call (CALL_STATE_IDLE). To see the logs in eclipse. Go to Window -> Show View -> Other -> Android -> LogCat
Hope it helps.
Sources
AndroidManifest.xml
ServiceReceiver.java
MyPhoneStateListener.java
References
BroadcastReceiver
9 comments:
Almond,
Thank you very much for posting your code. I am sure the code is for a season developer. However, I am just learning the android environment. I wanted to get some input from you, it possible. I'm trying to run your code "Phone state listener" I have included a link
http://almondmendoza.com/2009/01/22/get-phone-state-when-someone-is-calling-using-broadcastreceiver-example/
I don't understand how to get it to run to put simply. I create the classes, but I don't understand how do I get it to run.
Any help would be appreciated
If you can't help, no worries, I just thought I'd ask.
Frank
To explain further, from the xml there is receiver http://developer.android.com/guide/topics/manifest/receiver-element.html which would recieve an intent, which is filtered(PHONE_STATE), we then bind our receiver to the class of ServiceReceiver which would have a function called onReceive, then we listen to the the phone state. So putting them all into seperate class should work unless the current sdk had changed something, i havent tried out the code lately. Thanks
I use adb logcat on real device
I call android phone, the first time ringging, there one "RINGING" log messages
I hang up and call android again, this time there 2 "RINGING" log messages together
Everytime I call again, more RINGING messages appear.
It seems like a lot of instance have been created but didn't destroy.
How you used adb logcat on real device. Can you please explain?
adb -d logcat
Hi Almond,
Thy for sharing your first steps in Android. I'm also a new developer and I have some questions after running your program.
Is it normal that the first time I run the program in the emulator, I have only one 'ringing' and the second time that i have multiples "ringing" in my logcat.
Are you sure we have to re-create the PhoneStateListener each time we receive an intent ? Or maybe it's only the emulator which is crappy ?
What I would like to know is : Is there only one ringing or multiples ? If i read the documentation, I'd say only one though but you know ... i'm not so sure : http://developer.android.com/intl/fr/reference/android/telephony/TelephonyManager.html#CALL_STATE_RINGING
Thanks for your awesome website and for the time you will spent for answering.
To Fr and Michael: This example has a flaw in that it adds a listener for each broadcast intent. This is wrong.
You should register the listener in some context that fits (such as an activity or service) and keep the reference. If you want to use the above mentioned method (which you shouldn't), you must keep a reference to a listener and only add a reference if one isn't available.
Some intents, such as this listener, must be executed in code for the reason that you don't want 20 applications starting for no reason each time a call is received.
Hi i m new n i want to know can i change state of phone....means i want to create a stoplist and if any call comes from stop list i want stop dat call
plz explain me or give some code i m new
How do you make a user-type broadcast instead of a PHONE one.
I have a status-bar-app trying to talk to a widget.
Thanks Almond!
Post a Comment