2011-09-09 27 views
6

Tôi đã có một dịch vụ chạy một chuỗi. Chủ đề lưu một số dữ liệu trong một tệp (trong sdcard). Khi Android chuyển sang chế độ ngủ, tôi cần dịch vụ đó và chuỗi tiếp tục chạy. Tôi đã thử nó với một PARTIAL_WAKE_LOCK, nhưng nó không hoạt động; luồng dừng khi Android đang ngủ. Các khóa khác như FULL_WAKE_LOCK hoạt động, nhưng tôi cần sử dụng PARTIAL_WAKE_LOCK vì, trong tương lai, trong chuỗi đó tôi sẽ đọc từ một cổng nối tiếp và tôi không quan tâm đến màn hình tắt.PARTIAL_WAKE_LOCK và chuỗi đang chạy trong một dịch vụ

Tôi không biết liệu mình có gặp lỗi hay không, hoặc nếu tôi không hiểu PARTIAL_WAKE_LOCK. Ai đó có thể cho tôi biết tại sao giải pháp của tôi không bị xáo trộn?

Đây là một phần của mã của hoạt động chính, nơi dịch vụ được 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)); 
    } 
} 

Đây là mã của dịch vụ:

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

    } 


} 

Trả lời

9

Vâng, tôi sẽ trả lời câu hỏi của tôi . Mã của tôi là ok. Tôi đã phát hiện ra vài phút trước rằng vấn đề là việc thực hiện PowerManager trong máy tính bảng Eken M009S, (một máy tính bảng Trung Quốc), thiết bị mà tôi đã thực hiện các bài kiểm tra. Trong các thiết bị khác, như trong điện thoại di động của Samsung, PARTIAL_WAKE_LOCK hoạt động hoàn hảo.

Các vấn đề liên quan