Re: [android-developers] Re: TelephonyManager PhoneStateListener

2010-09-08 Thread KrcK ---
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
 

Re: [android-developers] Re: TelephonyManager PhoneStateListener

2010-09-08 Thread Mark Murphy
On Wed, Sep 8, 2010 at 5:55 AM, KrcK --- krc...@gmail.com wrote:
 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?

I do not completely understand what you wrote here, but I think you
have the right idea.

 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.

The BroadcastReceiver will be destroyed when onReceive() is finished,
and so you have no guarantees about the lifetime of any static data
members.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in London: http://skillsmatter.com/go/os-mobile-server

-- 
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


Re: [android-developers] Re: TelephonyManager PhoneStateListener

2010-09-08 Thread KrcK ---
The general idea is to create an app like incoming call log. So I have to
create a new register in my database when a new call is answered or lost.

To do that, I implement a BroadcastReceiver that reacts when the phone state
changes. In the onReceive method, I get the TelephonyManager and through
this I can get the call state (getCallSate method).

The problem here is that I can not know the previous state, so I can not
know if an incoming call was answered or not, while if I could get the
previous state, I would know it with the states transition.

Maybe I could get the information that I need through the Intent (that I get
in onReceive method) or through the TelephonyManager, but I do not know if
it is possible.

Any suggestion to do something like that?

Thanks.


2010/9/8 Mark Murphy mmur...@commonsware.com

 On Wed, Sep 8, 2010 at 5:55 AM, KrcK --- krc...@gmail.com wrote:
  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?

 I do not completely understand what you wrote here, but I think you
 have the right idea.

  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.

 The BroadcastReceiver will be destroyed when onReceive() is finished,
 and so you have no guarantees about the lifetime of any static data
 members.

 --
 Mark Murphy (a Commons Guy)
 http://commonsware.com | http://github.com/commonsguy
 http://commonsware.com/blog | http://twitter.com/commonsguy

 Android Training in London: http://skillsmatter.com/go/os-mobile-server

 --
 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.comandroid-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

Re: [android-developers] Re: TelephonyManager PhoneStateListener

2010-09-08 Thread Mark Murphy
On Wed, Sep 8, 2010 at 7:12 AM, KrcK --- krc...@gmail.com wrote:
 The general idea is to create an app like incoming call log. So I have to
 create a new register in my database when a new call is answered or lost.

Android has a CallLog already. It's called android.provider.CallLog.

 To do that, I implement a BroadcastReceiver that reacts when the phone state
 changes. In the onReceive method, I get the TelephonyManager and through
 this I can get the call state (getCallSate method).

There is also an Intent extra.

 The problem here is that I can not know the previous state, so I can not
 know if an incoming call was answered or not, while if I could get the
 previous state, I would know it with the states transition.

Why not use the existing CallLog?

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in London: http://skillsmatter.com/go/os-mobile-server

-- 
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


Re: [android-developers] Re: TelephonyManager PhoneStateListener

2010-09-08 Thread KrcK ---
 Why not use the existing CallLog?

Because I wanted to play with this API and I thougth to do that developing
an app like CallLog.

On the other hand, the CallLog could be deleted by the user and I want to
not allow it in my app.

 There is also an Intent extra.

Which information could I get through that intent?

Thanks for your help!


2010/9/8 Mark Murphy mmur...@commonsware.com

 On Wed, Sep 8, 2010 at 7:12 AM, KrcK --- krc...@gmail.com wrote:
  The general idea is to create an app like incoming call log. So I have to
  create a new register in my database when a new call is answered or lost.

 Android has a CallLog already. It's called android.provider.CallLog.

  To do that, I implement a BroadcastReceiver that reacts when the phone
 state
  changes. In the onReceive method, I get the TelephonyManager and through
  this I can get the call state (getCallSate method).


  The problem here is that I can not know the previous state, so I can not
  know if an incoming call was answered or not, while if I could get the
  previous state, I would know it with the states transition.



 --
 Mark Murphy (a Commons Guy)
 http://commonsware.com | http://github.com/commonsguy
 http://commonsware.com/blog | http://twitter.com/commonsguy

 Android Training in London: http://skillsmatter.com/go/os-mobile-server

 --
 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.comandroid-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

Re: [android-developers] Re: TelephonyManager PhoneStateListener

2010-09-08 Thread Mark Murphy
On Wed, Sep 8, 2010 at 7:36 AM, KrcK --- krc...@gmail.com wrote:
 There is also an Intent extra.

 Which information could I get through that intent?

The EXTRA_STATE extra indicates the new call state. If the new state
is RINGING, a second extra EXTRA_INCOMING_NUMBER provides the incoming
phone number as a String.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in London: http://skillsmatter.com/go/os-mobile-server

-- 
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


Re: [android-developers] Re: TelephonyManager PhoneStateListener

2010-09-08 Thread KrcK ---
Ok but how could I identify that I answered a call or is lost, and the
duration of the call? Because I can know the current state and at what time
it is produced but I do not have information about previous states.

For example, I answer a call so I will receive OFFHOOK state, but when the
call finished I will receive the IDLE state, but I could not know if the
previous state was RINGING or OFFHOOK to identify wich kind of call was
realized (answer or lost), and in the case of answered call I could not know
when started.

2010/9/8 Mark Murphy mmur...@commonsware.com

 On Wed, Sep 8, 2010 at 7:36 AM, KrcK --- krc...@gmail.com wrote:
  There is also an Intent extra.
 
  Which information could I get through that intent?

 The EXTRA_STATE extra indicates the new call state. If the new state
 is RINGING, a second extra EXTRA_INCOMING_NUMBER provides the incoming
 phone number as a String.

 --
 Mark Murphy (a Commons Guy)
 http://commonsware.com | http://github.com/commonsguy
 http://commonsware.com/blog | http://twitter.com/commonsguy

 Android Training in London: http://skillsmatter.com/go/os-mobile-server

 --
 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.comandroid-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

Re: [android-developers] Re: TelephonyManager PhoneStateListener

2010-09-08 Thread Mark Murphy
On Wed, Sep 8, 2010 at 8:18 AM, KrcK --- krc...@gmail.com wrote:
 Ok but how could I identify that I answered a call or is lost, and the
 duration of the call? Because I can know the current state and at what time
 it is produced but I do not have information about previous states.

The answered/lost data is not part of a public API, AFAIK. Duration of
the call is something you can track yourself.

 For example, I answer a call so I will receive OFFHOOK state, but when the
 call finished I will receive the IDLE state, but I could not know if the
 previous state was RINGING or OFFHOOK to identify wich kind of call was
 realized (answer or lost), and in the case of answered call I could not know
 when started.

Sure you can.

You have indicated that you are replicating the CallLog. I would
imagine this involves persisting the data. Hence, you already have
some file or database or something where you are storing data. Store
the in-progress call information using the same techniques.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in London: http://skillsmatter.com/go/os-mobile-server

-- 
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


Re: [android-developers] Re: TelephonyManager PhoneStateListener

2010-09-08 Thread KrcK ---
Ok, I think that now I know a way to do it!

Thanks Mark, your help and explanations were very helpful!

2010/9/8 Mark Murphy mmur...@commonsware.com

 On Wed, Sep 8, 2010 at 8:18 AM, KrcK --- krc...@gmail.com wrote:
  Ok but how could I identify that I answered a call or is lost, and the
  duration of the call? Because I can know the current state and at what
 time
  it is produced but I do not have information about previous states.

 The answered/lost data is not part of a public API, AFAIK. Duration of
 the call is something you can track yourself.

  For example, I answer a call so I will receive OFFHOOK state, but when
 the
  call finished I will receive the IDLE state, but I could not know if the
  previous state was RINGING or OFFHOOK to identify wich kind of call was
  realized (answer or lost), and in the case of answered call I could not
 know
  when started.

 Sure you can.

 You have indicated that you are replicating the CallLog. I would
 imagine this involves persisting the data. Hence, you already have
 some file or database or something where you are storing data. Store
 the in-progress call information using the same techniques.

 --
 Mark Murphy (a Commons Guy)
 http://commonsware.com | http://github.com/commonsguy
 http://commonsware.com/blog | http://twitter.com/commonsguy

 Android Training in London: http://skillsmatter.com/go/os-mobile-server

 --
 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.comandroid-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

[android-developers] Re: TelephonyManager PhoneStateListener

2010-09-07 Thread Indicator Veritatis
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
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en