Hi everybody,

I'm new to android, but I am picking up speed quickly. I've spent the
last several years writing Windows services. So I am not a complete
newbie in this area.

I've written a test service to understand how android implements a
service. I believe I understand the difference between
Context.startService and Context.bindService. If I am not mistaken,
Context.startService will tell android to keep this service running,
even after the Activity that started it has ended.

While testing the service, I have come across something interesting.

My problem is that the service appears to sleep or pause for a few
hours, run a short time, and then sleep for a few more hours, then run
a short time, etc when the phone has become inactive. In other words,
when I am doing things on the phone the service is doing what is is
supposed to. But after I have finished doing things on the phone and
the screen goes blank the service appears to sleep or pause. I don't
know if I have implemented the service wrong or if this is how Android
services work.

The test service I have written simply sets a postDelayed handler to
increment++ a variable in the Runnable every minute, and then write
the value of that variable and a timestamp to a file (log.txt) on the
sd card. This how I know that the service appears to sleep. Because
there are gaps in the timestamp that correspond to when the phone was
inactive. If the service were truly active the whole time, there
should be a line item in the log.txt file every minute.

>From the log.txt file below you can see that over an eleven hour
period there are only 36 line items with time stamps ranging from
10:27 PM through 9:23 AM. If the service was running and active the
whole time there should be over 600 lines in the text file. The phone
was on AC power the whole time so Android should not have tried to
stop services due to power issues. The phone was not used or touched
from about midnight to 9 the next morning, so there should not have
been a low resource clean up.

I have spent several days looking for an answer and everything I can
find seems to validate that when a service is started and not bound,
it will stay running (http://code.google.com/android/reference/android/
app/Service.html). However, it appears it does not. Unless I have
completely missed the ball on this.


I have included the service.java and activity.java as well as the log
file. Any help/ideas would be greatly appreciated. I am truly
stumped...




Here is a sample log.txt

02/05/2009 10:27:02 PM: Service Created.
02/05/2009 10:27:02 PM: Service Started.
02/05/2009 10:27:07 PM: iCount = 1
02/05/2009 10:28:07 PM: iCount = 2
02/05/2009 10:29:07 PM: iCount = 3
02/05/2009 11:20:49 PM: iCount = 4
02/05/2009 11:31:11 PM: iCount = 5
02/05/2009 11:32:11 PM: iCount = 6
02/05/2009 11:33:16 PM: iCount = 7
02/05/2009 11:34:16 PM: iCount = 8
02/05/2009 11:35:16 PM: iCount = 9
02/05/2009 11:36:16 PM: iCount = 10
02/05/2009 11:37:16 PM: iCount = 11
02/05/2009 11:38:16 PM: iCount = 12
02/05/2009 11:39:16 PM: iCount = 13
02/05/2009 11:40:16 PM: iCount = 14
02/05/2009 11:41:16 PM: iCount = 15
02/05/2009 11:42:16 PM: iCount = 16
02/05/2009 11:43:17 PM: iCount = 17
02/05/2009 11:44:17 PM: iCount = 18
02/05/2009 11:45:17 PM: iCount = 19
02/05/2009 11:46:17 PM: iCount = 20
02/05/2009 11:47:17 PM: iCount = 21
02/05/2009 11:48:17 PM: iCount = 22
02/05/2009 11:55:58 PM: iCount = 23
02/06/2009 01:06:56 AM: iCount = 24
02/06/2009 03:07:41 AM: iCount = 25
02/06/2009 04:38:32 AM: iCount = 26
02/06/2009 06:54:49 AM: iCount = 27
02/06/2009 08:03:14 AM: iCount = 28
02/06/2009 08:57:50 AM: iCount = 29
02/06/2009 08:58:50 AM: iCount = 30
02/06/2009 08:59:50 AM: iCount = 31
02/06/2009 09:05:24 AM: iCount = 32
02/06/2009 09:06:24 AM: iCount = 33
02/06/2009 09:07:24 AM: iCount = 34
02/06/2009 09:08:24 AM: iCount = 35
02/06/2009 09:23:09 AM: iCount = 36
02/06/2009 09:23:17 AM: Service Destroyed.







Here is the service java file:

Java:

package bSoft.bService;


import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;

public class bService extends Service
{
     private long _iCount = 0;


     private Handler objHandler = new Handler();



     @Override
     public void onCreate()
     {
          super.onCreate();

     SaveStatusData("Service Created.\n\n");
     }

    @Override
    public void onDestroy()
    {
     super.onDestroy();

     objHandler.removeCallbacks(doMonitoringTasks);

     SaveStatusData("Service Destroyed.\n\n");
    }


     @Override
     public void onStart(Intent intent, int startId)
     {
     SaveStatusData("Service Started.\n\n");

          objHandler.postDelayed(doMonitoringTasks, 5000);
     }

     @Override
     public IBinder onBind(Intent intent)
     {
          return binder;
     }


     private final IBinder binder = new MyBinder();

     public class MyBinder extends Binder
     {
          bService getService()
          {
               return bService.this;
          }
     }


     private Runnable doMonitoringTasks = new Runnable()
     {
          public void run()
          {
               _iCount++;

               SaveStatusData("iCount = " + String.valueOf(_iCount));

               objHandler.postDelayed(doMonitoringTasks, 60000);
          }
     };


     public boolean SaveStatusData(String strTextToWrite)
     {
          FileWriter objFileWriter = null;

          try
          {
               Calendar objCalendar = Calendar.getInstance();
               SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy
hh:mm:ss a");
               String strDate = sdf.format(objCalendar.getTime
());

              StringBuilder objStringBuilder = new StringBuilder();

              objStringBuilder.append(strDate);
              objStringBuilder.append(": ");
              objStringBuilder.append(strTextToWrite);
              objStringBuilder.append("\n");

              objFileWriter = new FileWriter("/sdcard/log.txt", true);
              objFileWriter.write(objStringBuilder.toString());
              objFileWriter.flush();
              objFileWriter.close();
          }
          catch (Exception e)
          {
               try
               {
                    objFileWriter.close();
               }
               catch (Exception e2) { /*Do Nothing*/ }

               return(false);
          }

          return(true);
     }
}




Here is the Activity java:

Java:

package bSoft.bService;



import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class bServiceGUI extends Activity
{
     private boolean blnServiceConnected = false;


     @Override
     public void onCreate(Bundle icicle)
     {

          super.onCreate(icicle);
          setContentView(R.layout.main);



          //Just started testing different ways to start the service.
          Intent objIntent = new Intent();
          objIntent.setClassName("bSoft.bService",
"bSoft.bService.bService");
          this.startService(objIntent);
//        this.startService(new Intent(this.get, bService.class));


     }


     private void ShowNotification(String strMsg, String strPosition)
     {
         Context context = getApplicationContext();

          Toast toast = Toast.makeText(context, strMsg, 100);

         if (strPosition.equals("BOTTOM"))
         {
          toast.setGravity(Gravity.BOTTOM, 0, 0);
         }

          toast.show();
     }


/
******************************************************************************************
     SERVICE STUFF
******************************************************************************************/

     private void startMyService()
     {
          ComponentName service = startService(new Intent(this,
bService.class));

          ShowNotification("Service Started", "BOTTOM");
     }

     private void stopMyService()
     {
          try
          {
              Intent i = new Intent();
              i.setClassName( "bSoft.bService",
"bSoft.bService.bService" );
              stopService( i );

               ShowNotification("Service Stopped", "BOTTOM");
          }
          catch (Exception e){ /* Do Nothing */ }
     }
}

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

Reply via email to