2011-09-09 16 views
6

Bir iş parçacığı çalıştıran bir servisim var. İş parçacığı bir dosyadaki bazı verileri (sdcard'ta) kaydeder. Android uyku moduna geçtiğinde, hizmete ve iş parçacığının çalışmaya devam etmesine ihtiyacım var. PARTIAL_WAKE_LOCK ile denedim, ancak işe yaramıyor; Android uyurken iş parçacığı durur. FULL_WAKE_LOCK gibi diğer kilitler çalışıyor, ancak PARTIAL_WAKE_LOCK'u kullanmam gerekiyor çünkü gelecekte bu serideki bir seri porttan okuyacağım ve ekranın kapanmadığı umurumda değil.PARTIAL_WAKE_LOCK ve bir hizmette çalışan iş parçacığı

Kodda bazı hatalar olup olmadığını veya PARTIAL_WAKE_LOCK değerini anlamadım mı bilmiyorum. Birisi bana çözümümün neden değişmediğini söyleyebilir?

Bu hizmet stareted ana faaliyet kodu parçasıdır:

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

    } 


} 

cevap

9

Eh, soruma cevap verecektir: Bu hizmetin kodudur

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

. Kodum tamam. Birkaç dakika önce sorunun, testler yaptığım cihaz olan Eken M009S tabletlerinde (Çinli bir tablet) PowerManager'ın uygulanması olduğunu keşfettim. Diğer cihazlarda, Samsung'un cep telefonlarında olduğu gibi, PARTIAL_WAKE_LOCK mükemmel çalışıyor.

İlgili konular