So, for example, If I want to create a new register with each incoming call in my database tableI should use a BroadcastReceiver, and if I want to do some actions in my app when appears an incoming call I should use a listener (like save some states or stop a service), is ok?
On the other hand I would like to know if is a good idea create static variables in a BroadCastReceiver because if I do not do it I can not know the duration of each answer call. Thanks for your explanations and help! 2010/9/8 Indicator Veritatis <mej1...@yahoo.com> > I think you have lost sight of the forest for the trees. The main > difference is that the Listener is just that -- a Listener. If you > don't use the Listener, you have to poll for changes in phone state or > create a Receiver. Often, this is a waste of time. The Listener should > be preferred, unless your needs are VERY simple, so that you can > afford the waste of time implied by polling for state change, or the > extra effort (as in your example) of making a whole Receiver. > > After all: except when it introduces nuisance complexity, fewer > application components is better. It is rare that you would want to > create a whole Receiver just for tracking phone state. Why bother, > when you can use a Listener? > > Besides: how do you plan to communicate between your Receiver and your > Activities? Generally, communicating with a Listener in the same > Activity or in the hosting process's thread is easier. You didn't even > try this communication in your example. > > On Sep 7, 1:09 am, KrcK --- <krc...@gmail.com> wrote: > > Hi all, > > > > I would like to know what are the main differences between > TelephonyManager > > and PhoneStateListener, because you can do (more or less) the same things > > with both. > > For example, you could create an incoming calls register implementing a > > broadcastreceiver and using TelephonyManger API or extending > > PhoneStateListener class and registering this new listener > > throughTelephonyManager . > > > > Case 1 - BroadcastReceiver (TelephonyManager) > > > > public class TestServiceReceiver extends BroadcastReceiver { > > @Override > > public void onReceive(Context context, Intent intent) { > > TelephonyManager telephony = (TelephonyManager) > > context.getSystemService(Context.TELEPHONY_SERVICE); > > int state = telephony.getCallState(); > > switch(state) { > > case TelephonyManager.CALL_STATE_IDLE: > > Log.d("TestServiceReceiver", "IDLE"); > > break; > > case TelephonyManager.CALL_STATE_OFFHOOK: > > Log.d("TestServiceReceiver", "OFFHOOK"); > > break; > > case TelephonyManager.CALL_STATE_RINGING: > > Log.d("TestServiceReceiver", "RINGING"); > > break; > > } > > > > } > > } > > > > <receiver android:name=".TestServiceReceiver"> > > <intent-filter> > > <action android:name="android.intent.action.PHONE_STATE" /> > > </intent-filter> > > </receiver> > > <uses-permission android:name="android.permission.READ_PHONE_STATE" /> > > > > Case 2 - PhoneStateListener > > > > public class TestServiceReceiver extends BroadcastReceiver { > > @Override > > public void onReceive(Context context, Intent intent) { > > TelephonyManager telephony = (TelephonyManager) > > context.getSystemService(Context.TELEPHONY_SERVICE); > > TestPhoneStateListener listener = > > new TestPhoneStateListener(); > > telephony.listen(phoneListener, > > PhoneStateListener.LISTEN_CALL_STATE); > > > > } > > } > > > > public class TestPhoneStateListener extends PhoneStateListener { > > public void onCallStateChanged(int state,String incomingNumber){ > > switch(state) { > > case TelephonyManager.CALL_STATE_IDLE: > > Log.d("TestPhoneStateListener ", "IDLE"); > > break; > > case TelephonyManager.CALL_STATE_OFFHOOK: > > Log.d("TestPhoneStateListener ", "OFFHOOK"); > > break; > > case TelephonyManager.CALL_STATE_RINGING: > > Log.d("TestPhoneStateListener ", "RINGING"); > > break; > > } > > > > } > > } > > > > <receiver android:name=".TestServiceReceiver"> > > <intent-filter> > > <action android:name="android.intent.action.PHONE_STATE" /> > > </intent-filter> > > </receiver> > > <uses-permission android:name="android.permission.READ_PHONE_STATE" /> > > > > In case 2, when the phone state changed, a new testphonestatelistener > will > > be registered (telephony.listen(phoneListener, > > PhoneStateListener.LISTEN_CALL_STATE)), but I could solve this problem > > convertingthe TestPhoneStateListener into a Singleton and > > using PhoneStateListener listener = > TestPhoneStateListener().getInstance() > > instead TestPhoneStateListener listener = new TestPhoneStateListener(). > > > > So my main question is when I have to use each one and for what purposes. > > > > Thanks and regards! > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to android-developers@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com> > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en