Can someone help me connect the dots? BroadcastReceiver(sms_received) + Device admin(lock screen) + disable Home/other menu buttons Activity

StackOverflow https://stackoverflow.com/questions/9781108

Question

I have more or less been able to understand and implement all these concepts in parts but somehow totally confused as what to put where and how do they fit together.

In short this is what I am trying to achieve on a non-rooted android phone

  1. Have an SMS broadcast receiver listen for incoming SMS with secret code
  2. Lock the screen if sms has keyword lock.

How do BroadCastReceivers and DeviceAdminRecivers go hand in hand?

Eg. I have the working code for intercepting an SMS. A switch case which calls a dummy(empty) lockScreen() function. Now where should I put DeviceAdminReceiver's code so that I can lock the screen (a new Activity with a password box with HOME and other menu buttons disabled) ?

So basically its from BroadcastReceiver -> Device Admin -> Activity.

Please help me.

Was it helpful?

Solution

The easiest way would be to create a custom HOME screen and add features mentioned below so that it looks like a custom lock screen :

  1. Remove Notification/Status bar from the custom LockScreen activity
  2. Catch and Disable HOME/MENU button clicks when LockScreen activity is visible
  3. Have and SMS broadcast receiver call the custom LockScreen activity when secret "LOCK PHONE" sms is received.
  4. Have a PhoneBroadstReceiver to make the LockScreen reappear with some delay(100ms) so that the incoming call system default screen doesn't push your lockScreenActivity to the background

P.S : DeviceAdmin can be used to lock your phone and show up the system default lock screen if the password has been set

OTHER TIPS

I don't believe you can create a custom lock screen on a non-rooted phone, because as far as I know, disabling the Home button is impossible from within an app.

Well, I wasn't aware of these lock-screen apps, but upon further research it seems that this would be the way to go:

1) Have your app register for the necessary SMS intent broadcasts.

2) Upon receiving the broadcast, check for the lock keyword

3) If lock keyword exists, programatically lock the phone using:

private void lockScreen(){
    KeyguardManager mgr = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
    KeyguardLock lock = mgr.newKeyguardLock(KEYGUARD_SERVICE); 
    lock.reenableKeyguard();
}

Of course, this doesn't use your custom lock screen, but it might not be necessary. If you do desire your own lock screen, then you should follow this guide

http://developer.android.com/guide/topics/admin/device-admin.html

to create one.

The code:

KeyguardManager mgr = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = mgr.newKeyguardLock(KEYGUARD_SERVICE); 
lock.reenableKeyguard();

will NOT lock the screen. It just enables the keyguard lock. When you run with

lock.disableKeyguard();

and press lock button on the device it will not lock the keyguard. To lock the screen programatically you have to refer to Device Admin and use locknow() method to lock immediately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top