2011-09-09 3 views
6

스레드를 실행하는 서비스가 있습니다. 스레드는 일부 데이터를 파일 (sdcard)에 저장합니다. Android가 잠자기 상태가되면 서비스가 계속 실행되고 스레드가 계속 실행되어야합니다. PARTIAL_WAKE_LOCK으로 시도했지만 작동하지 않습니다. 안드로이드가 잠자는 동안 스레드가 멈 춥니 다. FULL_WAKE_LOCK과 같은 다른 잠금 기능은 작동하지만 PARTIAL_WAKE_LOCK을 사용해야합니다. 장래에 해당 스레드에서 직렬 포트에서 읽을 것이므로 화면을 꺼도 상관 없습니다.PARTIAL_WAKE_LOCK 및 서비스에서 실행중인 스레드

코드에 실수가 있거나 PARTIAL_WAKE_LOCK을 (를) 이해할 수없는 경우 잘 모르겠습니다. 누군가 내 솔루션이 왜 깨지지 않는지 말할 수 있습니까?

이 서비스가 stareted 메인 활동의 코드의 일부입니다

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

    } 


} 

답변

9

음, 내 질문에 대답 할 것이다 :이 서비스의 코드가

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

. 내 코드는 괜찮아. 필자는 몇 분 전에 문제가 Eken M009S 태블릿 (Power Tab) (Power Tab은 중국 태블릿)에서 테스트를 수행 한 장치를 발견했습니다. 삼성의 휴대 전화와 같은 다른 기기에서는 PARTIAL_WAKE_LOCK이 완벽하게 작동합니다.

관련 문제