Search code examples
androidbroadcastreceiverandroid-manifest

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 Log and toast.

here is my complete code.

MyReceiver.java

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 {

@Override
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)
            .show();

    // perform what you want here

}

}

menifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.powerbuttontest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.powerbuttontest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".MyReceiver" >
        <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>
</application>
</manifest>

MainActivity.java

package com.example.powerbuttontest;

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

public class MainActivity extends Activity {

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

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

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

Solution

  • 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 {
    
            @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
                }
            }
    }
    

    and your receiver can be something

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