Click here to Skip to main content
14,128,445 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8K views
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

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:

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)

Share

About the Author

jit_knit
India India
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionHow to get screenshot from BroadcastReceiver? Pin
piyu8822-Apr-15 2:55
professionalpiyu8822-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.

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