[android-developers] Re: PARTIAL_WAKE_LOCK and thread running in a service

2011-09-12 Thread Evelyon
Well, I tried getting lock in the onCreate, but I have got the same
problem, it doesn't work. Any other idea?

And I know that a wakelock isn't very good for the battery life, but I
need to do it in this way. Anyway, thanks for the advice.


On 10 sep, 19:53, Dianne Hackborn hack...@android.com wrote:
 And note that holding a partial wake lock will have a *very* noticeable
 impact on battery life.



 On Fri, Sep 9, 2011 at 3:18 PM, nexbug gsuku...@gmail.com wrote:
  Get the lock in the oncreate of the service and release it at the end
  of the thread

  On Sep 9, 4:41 am, Evelyon evely...@gmail.com wrote:
   I have got a service which runs a thread. The thread save some data in
   a file (in the sdcard). When Android goes to sleep, I need that the
   service and the thread continue running. I tried it with a
   PARTIAL_WAKE_LOCK, but it doesn't work; the thread stops while Android
   is sleeping. Other locks like FULL_WAKE_LOCK works, but I need to use
   PARTIAL_WAKE_LOCK because, in the future, in that thread I will read
   from a serial port and I don't care the screen turn off.

   I don't know if I have some mistake in the code, or if I don't
   understand the PARTIAL_WAKE_LOCK. Somebody can tell me why my solution
   doesn't wrok?

   This is part of the code of the main activity, where the service is
   stareted:

       public void onClick(View v) {
           if (SerialPortService.WAKELOCK == null) {
               PowerManager pm = (PowerManager)
   getSystemService(Context.POWER_SERVICE);
               SerialPortService.WAKELOCK =
   pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
   SerialPortService.WL_TAG);
               SerialPortService.WAKELOCK.acquire();
               startService(new Intent(getApplicationContext(),
   SerialPortService.class));
           }
       }

   This is the code of the service:

       public class SerialPortService extends Service {

           public static String WL_TAG = serial_port_wl_tag;
           public static PowerManager.WakeLock WAKELOCK = null;
           private BufferedWriter out = null;

           public IBinder onBind(Intent intent) {
               return null;
           }

           public void onCreate() {
               super.onCreate();
               try {
                   File root = Environment.getExternalStorageDirectory();
                   if (root.canWrite()){
                       File dataFile = new File(root, batterytest.txt);
                       FileWriter dataFileWritter = new
   FileWriter(dataFile);
                       out = new BufferedWriter(dataFileWritter);
                   }
               } catch (IOException ioe) {
                   Log.d(TEST, Could not open file  +
   ioe.getMessage());
               }
               readThread = new ReadThread();
               readThread.start();
           }

           public void onDestroy() {
               if (readThread != null) readThread.interrupt();
               WAKELOCK.release();
               WAKELOCK = null;
               try {
                   out.close();
               } catch (IOException ioe) {
                   Log.d(TEST, Could not close file  +
   ioe.getMessage());
               }
               super.onDestroy();
           }

           private class ReadThread extends Thread {
               public void run() {
                   super.run();
                   while (!isInterrupted()) {
                       try {
                           Thread.sleep(5000);
                           if (out != null) {
                               Calendar now = Calendar.getInstance();
                               out.write(now.getTime().toString());
                               out.newLine();
                       } catch (IOException ioe) {
                           Log.d(TEST, Could not read file  +
   ioe.getMessage());}
                           return;
                       } catch (InterruptedException e) {
                           return;
                       }
                   }
               }

           }

       }

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

 --
 Dianne Hackborn
 Android framework engineer
 hack...@android.com

 Note: please don't send private questions to me, as I don't have time to
 provide private support, and so won't reply to such e-mails.  All such
 questions should be posted on public forums, where I and others can see and
 answer them.

-- 
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] Re: PARTIAL_WAKE_LOCK and thread running in a service

2011-09-12 Thread Evelyon
I've discovered few minutes ago that my code is ok. The problem is the
implementation of the PowerManager in the Eken M009S tablets, (a
chinese tablet). In other devices, like in Samsung's cell phones, the
PARTIAL_WAKE_LOCK works perfectly.


On 12 sep, 09:23, Evelyon evely...@gmail.com wrote:
 Well, I tried getting lock in the onCreate, but I have got the same
 problem, it doesn't work. Any other idea?

 And I know that a wakelock isn't very good for the battery life, but I
 need to do it in this way. Anyway, thanks for the advice.

 On 10 sep, 19:53, Dianne Hackborn hack...@android.com wrote:

  And note that holding a partial wake lock will have a *very* noticeable
  impact on battery life.

  On Fri, Sep 9, 2011 at 3:18 PM, nexbug gsuku...@gmail.com wrote:
   Get the lock in the oncreate of the service and release it at the end
   of the thread

   On Sep 9, 4:41 am, Evelyon evely...@gmail.com wrote:
I have got a service which runs a thread. The thread save some data in
a file (in the sdcard). When Android goes to sleep, I need that the
service and the thread continue running. I tried it with a
PARTIAL_WAKE_LOCK, but it doesn't work; the thread stops while Android
is sleeping. Other locks like FULL_WAKE_LOCK works, but I need to use
PARTIAL_WAKE_LOCK because, in the future, in that thread I will read
from a serial port and I don't care the screen turn off.

I don't know if I have some mistake in the code, or if I don't
understand the PARTIAL_WAKE_LOCK. Somebody can tell me why my solution
doesn't wrok?

This is part of the code of the main activity, where the service is
stareted:

    public void onClick(View v) {
        if (SerialPortService.WAKELOCK == null) {
            PowerManager pm = (PowerManager)
getSystemService(Context.POWER_SERVICE);
            SerialPortService.WAKELOCK =
pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
SerialPortService.WL_TAG);
            SerialPortService.WAKELOCK.acquire();
            startService(new Intent(getApplicationContext(),
SerialPortService.class));
        }
    }

This is the code of the service:

    public class SerialPortService extends Service {

        public static String WL_TAG = serial_port_wl_tag;
        public static PowerManager.WakeLock WAKELOCK = null;
        private BufferedWriter out = null;

        public IBinder onBind(Intent intent) {
            return null;
        }

        public void onCreate() {
            super.onCreate();
            try {
                File root = Environment.getExternalStorageDirectory();
                if (root.canWrite()){
                    File dataFile = new File(root, batterytest.txt);
                    FileWriter dataFileWritter = new
FileWriter(dataFile);
                    out = new BufferedWriter(dataFileWritter);
                }
            } catch (IOException ioe) {
                Log.d(TEST, Could not open file  +
ioe.getMessage());
            }
            readThread = new ReadThread();
            readThread.start();
        }

        public void onDestroy() {
            if (readThread != null) readThread.interrupt();
            WAKELOCK.release();
            WAKELOCK = null;
            try {
                out.close();
            } catch (IOException ioe) {
                Log.d(TEST, Could not close file  +
ioe.getMessage());
            }
            super.onDestroy();
        }

        private class ReadThread extends Thread {
            public void run() {
                super.run();
                while (!isInterrupted()) {
                    try {
                        Thread.sleep(5000);
                        if (out != null) {
                            Calendar now = Calendar.getInstance();
                            out.write(now.getTime().toString());
                            out.newLine();
                    } catch (IOException ioe) {
                        Log.d(TEST, Could not read file  +
ioe.getMessage());}
                        return;
                    } catch (InterruptedException e) {
                        return;
                    }
                }
            }

        }

    }

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

  --
  Dianne Hackborn
  Android framework engineer
  hack...@android.com

  Note: please don't send private questions to me, as I don't have 

Re: [android-developers] Re: PARTIAL_WAKE_LOCK and thread running in a service

2011-09-10 Thread Dianne Hackborn
And note that holding a partial wake lock will have a *very* noticeable
impact on battery life.

On Fri, Sep 9, 2011 at 3:18 PM, nexbug gsuku...@gmail.com wrote:

 Get the lock in the oncreate of the service and release it at the end
 of the thread

 On Sep 9, 4:41 am, Evelyon evely...@gmail.com wrote:
  I have got a service which runs a thread. The thread save some data in
  a file (in the sdcard). When Android goes to sleep, I need that the
  service and the thread continue running. I tried it with a
  PARTIAL_WAKE_LOCK, but it doesn't work; the thread stops while Android
  is sleeping. Other locks like FULL_WAKE_LOCK works, but I need to use
  PARTIAL_WAKE_LOCK because, in the future, in that thread I will read
  from a serial port and I don't care the screen turn off.
 
  I don't know if I have some mistake in the code, or if I don't
  understand the PARTIAL_WAKE_LOCK. Somebody can tell me why my solution
  doesn't wrok?
 
  This is part of the code of the main activity, where the service is
  stareted:
 
  public void onClick(View v) {
  if (SerialPortService.WAKELOCK == null) {
  PowerManager pm = (PowerManager)
  getSystemService(Context.POWER_SERVICE);
  SerialPortService.WAKELOCK =
  pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
  SerialPortService.WL_TAG);
  SerialPortService.WAKELOCK.acquire();
  startService(new Intent(getApplicationContext(),
  SerialPortService.class));
  }
  }
 
  This is the code of the service:
 
  public class SerialPortService extends Service {
 
  public static String WL_TAG = serial_port_wl_tag;
  public static PowerManager.WakeLock WAKELOCK = null;
  private BufferedWriter out = null;
 
  public IBinder onBind(Intent intent) {
  return null;
  }
 
  public void onCreate() {
  super.onCreate();
  try {
  File root = Environment.getExternalStorageDirectory();
  if (root.canWrite()){
  File dataFile = new File(root, batterytest.txt);
  FileWriter dataFileWritter = new
  FileWriter(dataFile);
  out = new BufferedWriter(dataFileWritter);
  }
  } catch (IOException ioe) {
  Log.d(TEST, Could not open file  +
  ioe.getMessage());
  }
  readThread = new ReadThread();
  readThread.start();
  }
 
  public void onDestroy() {
  if (readThread != null) readThread.interrupt();
  WAKELOCK.release();
  WAKELOCK = null;
  try {
  out.close();
  } catch (IOException ioe) {
  Log.d(TEST, Could not close file  +
  ioe.getMessage());
  }
  super.onDestroy();
  }
 
  private class ReadThread extends Thread {
  public void run() {
  super.run();
  while (!isInterrupted()) {
  try {
  Thread.sleep(5000);
  if (out != null) {
  Calendar now = Calendar.getInstance();
  out.write(now.getTime().toString());
  out.newLine();
  } catch (IOException ioe) {
  Log.d(TEST, Could not read file  +
  ioe.getMessage());}
  return;
  } catch (InterruptedException e) {
  return;
  }
  }
  }
 
  }
 
  }

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




-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
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: PARTIAL_WAKE_LOCK and thread running in a service

2011-09-09 Thread nexbug
Get the lock in the oncreate of the service and release it at the end
of the thread

On Sep 9, 4:41 am, Evelyon evely...@gmail.com wrote:
 I have got a service which runs a thread. The thread save some data in
 a file (in the sdcard). When Android goes to sleep, I need that the
 service and the thread continue running. I tried it with a
 PARTIAL_WAKE_LOCK, but it doesn't work; the thread stops while Android
 is sleeping. Other locks like FULL_WAKE_LOCK works, but I need to use
 PARTIAL_WAKE_LOCK because, in the future, in that thread I will read
 from a serial port and I don't care the screen turn off.

 I don't know if I have some mistake in the code, or if I don't
 understand the PARTIAL_WAKE_LOCK. Somebody can tell me why my solution
 doesn't wrok?

 This is part of the code of the main activity, where the service is
 stareted:

     public void onClick(View v) {
         if (SerialPortService.WAKELOCK == null) {
             PowerManager pm = (PowerManager)
 getSystemService(Context.POWER_SERVICE);
             SerialPortService.WAKELOCK =
 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
 SerialPortService.WL_TAG);
             SerialPortService.WAKELOCK.acquire();
             startService(new Intent(getApplicationContext(),
 SerialPortService.class));
         }
     }

 This is the code of the service:

     public class SerialPortService extends Service {

         public static String WL_TAG = serial_port_wl_tag;
         public static PowerManager.WakeLock WAKELOCK = null;
         private BufferedWriter out = null;

         public IBinder onBind(Intent intent) {
             return null;
         }

         public void onCreate() {
             super.onCreate();
             try {
                 File root = Environment.getExternalStorageDirectory();
                 if (root.canWrite()){
                     File dataFile = new File(root, batterytest.txt);
                     FileWriter dataFileWritter = new
 FileWriter(dataFile);
                     out = new BufferedWriter(dataFileWritter);
                 }
             } catch (IOException ioe) {
                 Log.d(TEST, Could not open file  +
 ioe.getMessage());
             }
             readThread = new ReadThread();
             readThread.start();
         }

         public void onDestroy() {
             if (readThread != null) readThread.interrupt();
             WAKELOCK.release();
             WAKELOCK = null;
             try {
                 out.close();
             } catch (IOException ioe) {
                 Log.d(TEST, Could not close file  +
 ioe.getMessage());
             }
             super.onDestroy();
         }

         private class ReadThread extends Thread {
             public void run() {
                 super.run();
                 while (!isInterrupted()) {
                     try {
                         Thread.sleep(5000);
                         if (out != null) {
                             Calendar now = Calendar.getInstance();
                             out.write(now.getTime().toString());
                             out.newLine();
                     } catch (IOException ioe) {
                         Log.d(TEST, Could not read file  +
 ioe.getMessage());}
                         return;
                     } catch (InterruptedException e) {
                         return;
                     }
                 }
             }

         }

     }

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