2011-09-09 9 views
6

Ho un servizio che esegue una discussione. Il thread salva alcuni dati in un file (nella sdcard). Quando Android va a dormire, ho bisogno che il servizio e il thread continuino a funzionare. Ho provato con un PARTIAL_WAKE_LOCK, ma non funziona; la discussione si interrompe mentre Android sta dormendo. Altri blocchi come FULL_WAKE_LOCK funzionano, ma ho bisogno di usare PARTIAL_WAKE_LOCK perché, in futuro, in quel thread leggerò da una porta seriale e non mi interessa che lo schermo si spenga.PARTIAL_WAKE_LOCK e thread in esecuzione in un servizio

Non so se ho qualche errore nel codice o se non capisco PARTIAL_WAKE_LOCK. Qualcuno può dirmi perché la mia soluzione non funziona?

Questo è parte del codice dell'attività principale, dove il servizio è 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)); 
    } 
} 

Questo è il codice del servizio:

public class SerialPortService extends Service { 

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

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

    } 


} 

risposta

9

Beh, io risponderò alla mia domanda . Il mio codice è ok Ho scoperto pochi minuti fa che il problema è l'implementazione di PowerManager nei tablet Eken M009S, (un tablet cinese), il dispositivo su cui ho effettuato i test. In altri dispositivi, come nei telefoni cellulari Samsung, PARTIAL_WAKE_LOCK funziona perfettamente.

Problemi correlati