Locking phone

rfresh

Well-Known Member
Licensed User
Longtime User
Is there code to lock my phone? I'm interested in this in case my phone is stolen and I'm wondering if I can lock it with my app?

I've searched the forum and the docs but didn't see anything about programatically locking your phone.

Thanks...
 

rfresh

Well-Known Member
Licensed User
Longtime User
I thought I'd bump this one since a week has gone by with no responses yet...thanks...
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Why not just install Avast Mobile Security and enable it's anti-theft module?

I have that installed and activated on my ZTE Blade and am pretty confident that only a thief with a good knowledge of flashing a new ROM could use my mobile if they stole it.

Martin.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I'd like to try and implement this capability on my apps for my own learning and knowledge with B4A.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
OK.

But there's many things to consider.

The first thing is how to stop a thief from simply downloading the latest official firmware for your mobile and simply (re)flashing your mobile.

Your code would have to somehow survive such a reflash.

Avast has two anti theft modes, one for rooted devices and one for non-rooted devices.

The mode for non-rooted devices is far less secure than the mode for rooted devices as Avast doesn't have the permission to write system files to make a complete reflash less effective.

Whereas the mode for rooted devices employs various techniques to write to the system partition and hopefully make a reflash noneffective in wiping Avast from the device.

I'd suggest you install Avast on a device and go through the anti theft setp up procedure to get some ideas about what steps you need to take to fully protect your device.

Then look at implementing those steps in your own application - and remove Avast when it's no longer required.

Martin.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
>The first thing is how to stop a thief from simply downloading the
>latest official firmware for your mobile and simply (re)flashing your mobile.

If the person who steals my phone knows how to install new firmware by re-flashing my phone, then more power to them.

I'm just interested in being able to Lock my phone (via code and without rooting) from someone who isn't an Android developer or Linux geek. In other words, from a casual smart phone user.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
It's been a two weeks so I thought I'd bump this topic on phone locking. Anyone know if we can lock a phone using code?

Thanks...
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I read that we cannot lock our phones as a security feature, but we can lock the screen using this pure Android java code:

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

The comment with this code was that this will also require the "Disable Keyguard" permission in your manifest file.

Can someone convert this code into something B4A can use? I'd like to test it and see if it will screen blank my Droid2 phone.

The article I read was here.

BTW, I notice that some apps in the Android Play store say they can lock your phone. I wonder if they can do that only for rooted phones?

Thanks...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is the equivalent code:
B4X:
Sub LockScreen
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod2("getSystemService", "keyguard", "java.lang.String")
   r.Target = r.RunMethod2("newKeyguardLock", "keyguard", "java.lang.String")
   r.RunMethod("reenableKeyguard")
End Sub
You should add this code to the manifest editor:
B4X:
AddPermission(android.permission.DISABLE_KEYGUARD)

It didn't work on the device I tested it (maybe because I don't have a password set).
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Thanks Erel. I will try this out on my Droid2 and Droid3 later today.

Update: I could not get it to lock my screen either. I set a PIN security code. Tested that it works. Then ran your code. The screen didn't lock. This was on my Droid2. I did the same thing on my Droid3 with the same results: nothing happened. And I did add the manifest statement.
 
Last edited:
Upvote 0

salmander

Active Member
Licensed User
Longtime User
Hi rfresh,

I am guessing you still haven't found a working solution yet. So I am posting my findings on this topic. I have managed to disable the keyguard, but now I can't re-enable it. Well here is my code;
B4X:
Sub keyguard(enable As Boolean)
    Log("keyguard sub initiated")
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "keyguard", "java.lang.String")
    r.Target = r.RunMethod2("newKeyguardLock", "keyguard", "java.lang.String")
    If enable = False Then
   Log("disabling the keyguard")
       r.RunMethod("disableKeyguard")
    Else
   Log("enabling the keyguard")
   r.RunMethod("reenableKeyguard")
    End If
End Sub

Hope this helps, and if you do find out how to re-enable the lock-screen, do post back please.
 
Upvote 0

salmander

Active Member
Licensed User
Longtime User
Thanks Erel, I was thinking of the same thing. But this should work according to android developer documentation. Have you tested it?

cheers
 
Upvote 0

salmander

Active Member
Licensed User
Longtime User
This is the equivalent code:
B4X:
   r.RunMethod("reenableKeyguard")
It didn't work on the device I tested it (maybe because I don't have a password set).


Note that you and rfresh were using "reenableKeyguard" method to disable the lock screen, which is actually use to enable the lock-screen. If you use my code, it should work.

I tested on tablet and my phone and both are working with disabling the lock screen. But the "reenableKeyguard" method seems to be not working.
 
Upvote 0

salmander

Active Member
Licensed User
Longtime User
I just tested the administrator library to re-enable the keylock. All it does is turns off the screen, when you turn it back on , the screen lock doesn't shows up and the activity resumes. I checked it few times if the application is set as administrator and indeed it is. I believe its the reenableKeyguard method. If I close the application using ExitApplication, then the Lock Screen behaves normally (as in shows after the screen turns on).
 
Upvote 0
Top