Click here to Skip to main content
15,881,588 members
Articles / Mobile Apps / Android
Tip/Trick

Using Hardware Keys on Android for Security Apps

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
20 Apr 2015CPOL 10.6K   6   1
Using Hardware Keys on Android for Security Apps

Introduction

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.

Background

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:

Java
MyService.java
-------
import android.app.Service;
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() {
    }
 
    @Override
    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",
                    Toast.LENGTH_SHORT).show();
        } else {
            Log.v("MyService", "onStartCommand Screen Off");
            Toast.makeText(getApplicationContext(),"onStartCommand Screen On",
                    Toast.LENGTH_SHORT).show();
        }
        return super.onStartCommand(intent,flags,startId);
    }
    @Override
    public void onCreate() {
        Context objContext = getApplicationContext();
        Log.v("MyService", "onCreate Start");
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new MyReceiver(objContext);
        registerReceiver(mReceiver, filter);
        Log.v("MyService", "onCreate end");
    }
}

--------------
MyReceiver.java
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;

    @Override
    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",
                    Toast.LENGTH_SHORT).show();
        }
        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",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to get screenshot from BroadcastReceiver? Pin
Shruti9122-Apr-15 2:55
professionalShruti9122-Apr-15 2:55 

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.