Do I using GregorianCalendar in the right way?

by Wang He » Fri, 06 Mar 2009 18:00:00 GMT


Sponsored Links
 0. I setup my Android emulator time zone to: Australia/Adelaide GMT
+10:30
1. My code snippet:

        mtz = new SimpleTimeZone(8 * 60 * 60 * 1000, "Asia/Shanghai");
        GregorianCalendar gc1 = new GregorianCalendar(); //using default,
that is Adelaide
        GregorianCalendar gc2 = new GregorianCalendar(mtz); //using my
customized time zone

        Date d1 = gc1.getTime();
        Log.v(TAG, "testCalendar2: d1.toString: "+d1.toString());
        Log.v(TAG, "testCalendar2: d1.toGMTString: "+d1.toGMTString());

        Date d2 = gc2.getTime();
        Log.v(TAG, "testCalendar2: d2.toString: "+d2.toString());
        Log.v(TAG, "testCalendar2: d2.toGMTString: "+d2.toGMTString());

2. My Log.v
20:11:04.807: VERBOSE/worldclock(669): testCalendar2: d1.toString: Fri
Mar 06 20:10:55 GMT+10:30 2009
20:11:04.927: VERBOSE/worldclock(669): testCalendar2: d1.toGMTString:
6 Mar 2009 09:40:55 GMT
20:11:13.967: VERBOSE/worldclock(669): testCalendar2: d2.toString: Fri
Mar 06 20:10:55 GMT+10:30 2009
20:11:14.277: VERBOSE/worldclock(669): testCalendar2: d2.toGMTString:
6 Mar 2009 09:40:55 GMT

3. I supposed to think d2 should be the Asia/Shanghai datetime, but
GregorianCalendar.getTime always returns the default time zone based
datetime. So:

Do I using the GregorianCalendar in the right way?
Why will GregorianCalendar provides a timezone based constructor?

BRs
He

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Delay in sound playback????

Hello I've been working on a soundboard type device that plays various
mp3 files on screen taps, but the problem i'm stuck with right now is
there is a delay after the screeen is touched...

--~--~---------~--~----~------------~-------~--~----~

2. Context.startService - The service does not stay running...

I rarely post to groups, because I can usually find the answer
somewhere on the web. So I apologize in advance if I have violated any
ettiquette. But this time I am truly stumped, and would appreciate any
help, insight, or suggestion from a more experienced Android
developer.

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 the service running,
even after the Activity that started it has ended.

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

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 it 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 timstamp 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.

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 the log.txt file

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 */ }
     }
}

--~--~---------~--~----~------------~-------~--~----~

3. Strange problem with this board

4. Does Android support JSR 177 APDUConnection?

5. Checking whether the screen is locked

6. what is difference between OnClickListener and OnTouchListener?

7. Context menus for TableRow?