2014-05-09 45 views
3

我想接收電源按鈕按鍵,但我無法接收它。Android電源按鈕按下不被接收器接收

接收器。

public class PowerSendMessage extends BroadcastReceiver 
{ 
public PowerSendMessage(Activity activity) 
    { 
     this.activity=activity; 
    } 
static int countPowerOff = 0; 
private Activity activity = null; 
@Override 
public void onReceive(Context context, Intent intent) 
{ 
    Log.d("Power Button", "Click"); 

    if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
    { 
     countPowerOff++; 
    } 
    else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
    { 
     if(countPowerOff == 2) 
     { 
      Log.d("Power Click", "2 Times"); 
     } 
    } 
} 

清單條目

<receiver android:name=".PowerSendMessage"> 

     <intent-filter> 
      <action android:name="android.intent.action.SCREEN_OFF"></action> 
      <action android:name="android.intent.action.SCREEN_ON"></action> 
      <action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action> 
      <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action> 
      <action android:name="android.intent.action.ACTION_SHUTDOWN"></action> 
     </intent-filter> 
    </receiver> 

如果把下面的代碼(指在文件)

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
    filter.addAction(Intent.ACTION_SCREEN_OFF); 
    PowerSendMessage mReceiver = new PowerSendMessage(this); 
    registerReceiver(mReceiver, filter); 

任何許可需要這個程序?

我想我錯過了什麼,但是我不知道請幫助我。

+0

試試我的回答omplete例子,適合它的工作:) – Hardik

+0

我編輯我的回答希望這將有助於 – Hardik

回答

9

試試這個

MyReceiver.java

public class MyReceiver extends BroadcastReceiver 
{ 
    private static int countPowerOff = 0; 

    public MyReceiver() 
    { 

    } 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
     {  
      Log.e("In on receive", "In Method: ACTION_SCREEN_OFF"); 
      countPowerOff++; 
     } 
     else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
     { 
      Log.e("In on receive", "In Method: ACTION_SCREEN_ON"); 
     } 
     else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)) 
     { 
      Log.e("In on receive", "In Method: ACTION_USER_PRESENT"); 
      if (countPowerOff > 2) 
      { 
       countPowerOff=0; 
       Toast.makeText(context, "MAIN ACTIVITY IS BEING CALLED ", Toast.LENGTH_LONG).show(); 
       Intent i = new Intent(context, MainActivity.class); 
       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       context.startActivity(i); 
      } 
     } 
    } 
} 

MainActivity.java

public class MainActivity extends Activity 
{ 
    private MyReceiver myReceiver; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
     filter.addAction(Intent.ACTION_SCREEN_OFF); 
     filter.addAction(Intent.ACTION_USER_PRESENT); 
     myReceiver = new MyReceiver(); 
     registerReceiver(myReceiver, filter); 
    } 

    @Override 
    protected void onDestroy() 
    { 
     if (myReceiver != null) 
     { 
      unregisterReceiver(myReceiver); 
      myReceiver = null; 
     }   
     super.onDestroy(); 
    } 
} 

沒有必要在清單中註冊的接收器,如果你有已在活動中完成。

另外,最好從ACTION_USER_PRESENT開始您的活動,因爲這意味着設備在被電源按鈕鎖定後被解鎖。但是,如果您想從ACTION_SCREEN_ON開始活動,那麼也可以這樣做,這也可以,但只有在用戶解鎖設備後才能看到活動。

編輯

,如果你想接收器,接收廣播從外部應用程序事件時,可以使用服務爲

首先,與其他廣泛的鑄造意圖,爲Intent.ACTION_SCREEN_OFF和Intent.ACTION_SCREEN_ON你不能在你的Android Manifest中聲明它們!所以,你需要做,這將繼續運行這樣

public static class UpdateService extends Service { 

     @Override 
     public void onCreate() { 
      super.onCreate(); 
      // register receiver that handles screen on and screen off logic 
      IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
      filter.addAction(Intent.ACTION_SCREEN_OFF); 
      BroadcastReceiver mReceiver = new Receiver(); 
      registerReceiver(mReceiver, filter); 
     } 

     @Override 
     public void onStart(Intent intent, int startId) { 
      boolean screenOn = intent.getBooleanExtra("screen_state", false); 
      if (!screenOn) { 
       // your code 
      } else { 
       // your code 
      } 
     } 
} 

服務和接收器可以是一些

public class Receiver extends BroadcastReceiver { 

    private boolean screenOff; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
      screenOff = true; 
     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
      screenOff = false; 
     } 
     Intent i = new Intent(context, UpdateService.class); 
     i.putExtra("screen_state", screenOff); 
     context.startService(i); 
    } 

} 

您需要添加 <service android:name=". UpdateService"/>

並運行活動這項服務通過

context.startService(new Intent(this, UpdateService.Class)); 

這裏是c爲您的要求

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

+1

你的代碼工作,但如果我要開始它甚至我的應用程序是沒有運行。如果活動關閉,接收器將無法工作。給我一些建議。 –

+1

使用服務在後臺運行並激活接收器。讓我知道它是否可以幫助你。我也在做類似的事情。 –

+0

使用粘性服務@VijayBarbhaya – TapanHP