5

I am trying to receive power button key press, But i unable to receive it.

Receiver.

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

Manifest Entry

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

Where to put below code (means in which file)

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

Any permission require for this procedure ?

I think I missing something but what that I dont Know please help me.

Vijay Barbhaya
  • 876
  • 1
  • 6
  • 14

0 Answers0