Click here to Skip to main content
14,207,778 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 20 Apr 2015
Licenced CPOL

Using Hardware Keys on Android for Security Apps

, 20 Apr 2015
Rate this:
Please Sign up or sign in to vote.
Using Hardware Keys on Android for Security Apps


Android is one of the largest available platforms in today’s world. Let’s use this platform to implement a service which will send the SMS alerts to your family and friends on hardware key pressed in specific pattern. Here, in this example, we have used toast in place of SMS send. You can easily replace this using the SMS code snippet.


There are many scenarios where-in one can use the mobile device for security purposes, one can send security alert to their family and friends using the following method, here we have used power on/off key to launch a toast.

Using the Code

Here is the basic service code that needs to be implemented to capture power on/off key and launch a toast on the screen:
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
    public MyService() {
    public int onStartCommand(Intent intent, int flags, int startId){
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
            Log.v("MyService", "onStartCommand Screen On");
            Toast.makeText(getApplicationContext(),"onStartCommand Screen On",
        } else {
            Log.v("MyService", "onStartCommand Screen Off");
            Toast.makeText(getApplicationContext(),"onStartCommand Screen On",
        return super.onStartCommand(intent,flags,startId);
    public void onCreate() {
        Context objContext = getApplicationContext();
        Log.v("MyService", "onCreate Start");
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        BroadcastReceiver mReceiver = new MyReceiver(objContext);
        registerReceiver(mReceiver, filter);
        Log.v("MyService", "onCreate end");

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

public class MyReceiver extends BroadcastReceiver {

    Context appContext;
    public MyReceiver(Context objContext) {
        appContext = objContext;

    public boolean wasScreenOn = true;
    public int nOnScreenCount = 0;

    public void onReceive(Context context, Intent intent) {
        // Implement code here to be performed when
        // broadcast is detected
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            wasScreenOn = false;
            Log.v("MyReceiver", "CASE ACTION_SCREEN_OFF");
            Toast.makeText(appContext,"MyReceiver ACTION_SCREEN_OFF",
        else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            wasScreenOn = true;
            Log.v("MyReceiver", "CASE ACTION_SCREEN_ON");
            Toast.makeText(appContext,"MyReceiver ACTION_SCREEN_ON",


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
No Biography provided

Comments and Discussions

QuestionHow to get screenshot from BroadcastReceiver? Pin
piyu8822-Apr-15 2:55
professionalpiyu8822-Apr-15 2:55 
how to get screen shot of phone screen from BroadcastReceiver?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190612.1 | Last Updated 20 Apr 2015
Article Copyright 2015 by jit_knit
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid