Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Using Hardware Keys on Android for Security Apps

5.00/5 (4 votes)
20 Apr 2015CPOL 11K  
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)