-2

I am currently trying to create an app that can track how much time I spend in a phone call and then display that on a toast message after clicking a button.

Code found here: http://paste.ideaslabs.com/show/6INd0afyi

I can't seem to figure out why the app is not working...

The idea is to create a service that starts as soon as I make a phone call (and keeps running indefinitely from then on). The Service has two while loops that track the start time and end time of the conversation using the getCallState() method through the TelephonyManager class. And then the values the end time and start time variables are stored and used in the activity class.

The activity class simply uses a button to display a toast message that says how much time I have spent.

When I try to run the app on my phone I can see the service running, but the app crashes sometimes or just shows that the time spent calling is 0 mins (which is not true..)

Hope you guys can point out any mistakes?!

Thanks!

fouadalnoor
  • 197
  • 2
  • 5
  • 14
  • 2
    Post your `CallService` code. – iTurki Jul 28 '12 at 18:07
  • 1
    ... and the logcat errors. Also please edit them directly into this question; highlight your code and press Ctrl+K to format code blocks properly. – Sam Jul 28 '12 at 18:07

3 Answers3

1

Just by seeing the code you have posted I would say that you have not read properly the docs about services. You don't create a service by doing a MyService s = new MyService()

Read the Android developer guide or the Android SDK documentation. You'll see for instance how to start a local service or to use intents to start a service.

E.g.:

Intent intent = new Intent(this, HelloService.class);
startService(intent);
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
  • Hi, The "final CallService cs= new CallService();" bit is only used to access the variables "EndTime" and "StartTime". I was not trying to start the service using my activity. It starts by using the "CallReceiver" class when a new call is made. Is it wrong to access the variables the way I did? – fouadalnoor Jul 28 '12 at 19:23
1

There are some events that the Operating System broadcast when they happen. eg. receiving sms, phone call state( sending, receiving). By reading your post, i think you should register your app with broadcast receiver. Here is a sample code.

public class PhoneCallState extends BroadcastReceiver 
{

static long start_time, end_time;

@Override
public void onReceive(Context context, Intent intent) 
{
   final Bundle extras = intent.getExtras();

   if(intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED))
   {            
      final String state = extras.getString(TelephonyManager.EXTRA_STATE);

      if ("RINGING".equals(state))
      {
        Toast.makeText(context, "Ringing", Toast.LENGTH_LONG).show();
      }       

      if ("OFFHOOK".equals(state))
      {

        start_time = System.currentTimeMillis();
        Toast.makeText(context, "Off", Toast.LENGTH_LONG).show();
      }


      if ("IDLE".equals(state))
      {

        end_time = System.currentTimeMillis();

        long duration = (end_time - start_time) /1000;
        Toast.makeText(context, "Duration : " + duration, Toast.LENGTH_LONG).show();

      }


   }
}

And register your receiver in the manifest file.

<receiver android:name=".PhoneCallState">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

}

Finally don't forgate to add a PHONE_STATE Permission.

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Imdad Sarkar
  • 1,245
  • 2
  • 12
  • 24
0

Looking at your previous questions I suggest reading this: How to make a phone call in android and come back to my activity when the call is done?

It describes how to set up a PhoneStateListener so that you can receive an intent when a call is started locally, received from someone else, and ended.

The Service has two while loops that track the start time and end time

These while loops are unnecessary with a PhoneStateListener, you can simply get two time stamps and subtract the difference, without having two while loops running every millisecond.

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179