2014-05-09 40 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