2011-09-09 17 views
7

स्क्रीन बंद होने पर (लॉक) होने पर मैं वाईफाई बंद करने की कोशिश कर रहा हूं, और स्क्रीन चालू होने पर इसे फिर से चालू कर सकता हूं (अनलॉक)।ACTION_SCREEN_ON और ACTION_SCREEN_OFF काम नहीं कर रहे हैं?

मैंने BroadcastReceiver बनाया; इस कोड को प्रकट में डाल:

<receiver android:name="MyIntentReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <action android:name="android.intent.action.SCREEN_OFF" /> 
       <action android:name="android.intent.action.SCREEN_ON" /> 
       <action android:name="android.intent.action.USER_PRESENT" /> 
       <category android:name="android.intent.category.HOME" /> 
       <category android:name="android.intent.category.LAUNCHER" />  
      </intent-filter> 
     </receiver> 

और इस वर्ग के MyIntentReceiver है:

package org.androidpeople.boot; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class MyIntentReceiver extends BroadcastReceiver { 
    // Called when boot completes 

    public static boolean startup; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Set what activity should launch after boot completes 

     System.out.println("Intent Action: " + intent.getAction()); 

     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 

      System.out.println("locked : ACTION_SCREEN_OFF"); 

     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 

      System.out.println("not locked : ACTION_SCREEN_ON "); 

     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 

      System.out.println("User Unlocking it "); 

     } 
     else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
      // this to indicate that program is running 
      // automaticlly not manually by user 
      startup = true; 
      System.out.println("Automatic BOOT at StartUp"); 

      Intent startupBootIntent = new Intent(context, LaunchActivity.class); 
      startupBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(startupBootIntent); 
     } 

    } 
} 

और परिणाम है - दोनों ACTION_SCREEN_ON और ACTION_SCREEN_OFF निकाल दिया कभी नहीं! USER_PRESENT और BOOT_COMPLETED ठीक काम किया लेकिन दूसरा नहीं था। मैं एक एमुलेटर का उपयोग कर रहा हूं, वास्तविक उपकरण नहीं - क्या इससे समस्या हो सकती है?

कोई मदद? बैटरी बचाने के लिए वाईफाई को सक्षम/अक्षम करने के लिए मुझे स्क्रीन को चालू और बंद करने की आवश्यकता है।

अग्रिम धन्यवाद

+0

http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/ –

उत्तर

8

में आप एक्सएमएल के माध्यम से उन उद्देश्यों को नहीं पकड़ सकते (मैं क्यों भूल जाते हैं)। हालांकि, अगर आप एक Service कि अपनी onStartCommand() में एक BroadcastReceiver सदस्य पंजीकृत करता है और उसके onDestroy() में यह unregisters इस्तेमाल कर सकते हैं। इसके लिए पृष्ठभूमि में लगातार चलने वाली सेवा की आवश्यकता होगी, जब तक आपको इसकी आवश्यकता हो, इसलिए वैकल्पिक मार्गों का पता लगाना सुनिश्चित करें।

तुम इतनी तरह अपने Service कक्षा में BroadcastReceiver निर्धारित कर सकते हैं:

private final class ScreenReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(final Context context, final Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
      //stuff 
     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
      //other stuff 
     } 
    } 
} 

एक थोड़ा जटिल उदाहरण के लिए, लेकिन एक पता चलता है कि कैसे BroadcastReceiver और Service इंटरैक्ट करते हैं मेरे ऐप, ElectricSleep से CheckForScreenBugAccelerometerService

12

SCREEN_OFF और SCREEN_ON कार्यों (और शायद अन्य) आप, कोड द्वारा BroadcastReceiver कॉन्फ़िगर करने के लिए प्रकट के माध्यम से नहीं किया है पर कब्जा करने के लिए।

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
intentFilter.addAction(Intent.ACTION_SCREEN_OFF); 
BroadcastReceiver mReceiver = new ScreenStateBroadcastReceiver(); 
registerReceiver(mReceiver, intentFilter); 

यह परीक्षण और सही काम करता है।

+0

+1, IntentFilter के लिए आप ON और कार्रवाई के लिए OFF का उपयोग करते हैं? –

+0

दोनों कार्य IntentFilter पर सेट हैं। – PoOk

+0

मैंने इस तरह से जोड़ा है लेकिन जब मैं हालिया ऐप को साफ़ करता हूं तो ब्रॉडकास्ट रिसीवर की विधि पर प्रतिक्रिया नहीं होती है। यदि ऐप चल रहा है तो रिसीव ठीक काम करता है। –

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