Qadir Hussain Qadir Hussain - 1 year ago 75
Android Question

how to start the app on power button press

I want to start my app when a user press the power button. I m following This code
but its not showing any

and toast.

here is my complete code.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

Log.v("onReceive", "Power button is pressed.");

Toast.makeText(context, "power button clicked", Toast.LENGTH_LONG)

// perform what you want here



menifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
android:versionName="1.0" >

android:targetSdkVersion="17" />

android:theme="@style/AppTheme" >
android:label="@string/app_name" >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
<receiver android:name=".MyReceiver" >
<action android:name="android.intent.action.SCREEN_OFF" >
<action android:name="android.intent.action.SCREEN_ON" >
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" >
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" >
<action android:name="android.intent.action.ACTION_SHUTDOWN" >

package com.example.powerbuttontest;

import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;


  • I think i m committing a mistake in my
    menifest file
    . please have a look on this. thanks.

Answer Source

First, unlike other broad casted intents, for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON you CANNOT declare them in your Android Manifest! so You need to make a service which will keep on running like this

public static class UpdateService extends Service {

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

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

and your receiver can be something

public class Receiver extends BroadcastReceiver {

    private boolean screenOff;

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download