스레드를 실행하는 서비스가 있습니다. 스레드는 일부 데이터를 파일 (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;
}
}
}
}
}