2011-09-09 14 views
6

मुझे एक सेवा मिली है जो थ्रेड चलाती है। थ्रेड फ़ाइल में कुछ डेटा सहेजता है (एसडीकार्ड में)। जब एंड्रॉइड सो जाता है, तो मुझे यह चाहिए कि सेवा और धागा चल रहा है। मैंने इसे 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)); 
    } 
} 

इस सेवा का कोड है । मेरा कोड ठीक है। मुझे कुछ मिनट पहले पता चला है कि समस्या एकेन M009S टैबलेट, (एक चीनी टैबलेट) में PowerManager का कार्यान्वयन है, जिस डिवाइस पर मैंने परीक्षण किया है। अन्य उपकरणों में, सैमसंग के सेल फोन की तरह, PARTIAL_WAKE_LOCK पूरी तरह से काम करता है।

संबंधित मुद्दे