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