Question

Things are simple: press an icon and send the phone to standby. The frustrating part comes from the phone: sometimes, randomly instead of remaining in standby, the screen awakes and the lock screen is displayed. I have not yet found any conditions to trigger this behavior, somewhere around one out of 3-5 tries. The app has admin permission and curiously on a custom ROM worked fine but on stock JellyBean 4.1.1 does not.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        deviceManger = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        compName = new ComponentName(this, MyAdmin.class);

        boolean active = deviceManger.isAdminActive(compName);
        if (active) {
            finish();
            deviceManger.lockNow();
      }
   }

I have my xml policies file like

<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
    <uses-policies>
        <force-lock>
        </force-lock>
    </uses-policies>
</device-admin>

Android manifest seems to be ok

<receiver
            android:name=".MyAdmin"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@layout/policies" >
                <intent-filter>
                    <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" >
                    </action>
                </intent-filter>
            </meta-data>
        </receiver>

The thing is I can't figure out why is this happening. This is happening on a Galaxy Nexus with stock, clean installed 4.1.1 As you can see the code is plain simple. I could try to make a Handler with a postDelayed of 500ms and run lockNow() again... but what if 500ms is not enough on some devices ?

When the lock screen appears, the logcat has:

07-26 20:45:27.446: I/ActivityManager(300): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.alinberce.standbytouch/.StandByActivity bnds=[168,1040][296,1168] u=0} from pid 524
07-26 20:45:27.634: D/dalvikvm(300): GC_FOR_ALLOC freed 1695K, 21% free 20113K/25351K, paused 69ms, total 69ms
07-26 20:45:27.650: D/SurfaceFlinger(124): About to give-up screen, flinger = 0x41b48af0
07-26 20:45:27.665: V/LockPatternKeyguardView(300): Set visibility on com.android.internal.policy.impl.LockPatternKeyguardView$4@41e57948 to 8388608
07-26 20:45:27.790: V/TransportControlView(300): Create TCV com.android.internal.widget.TransportControlView@419f9b08
07-26 20:45:27.861: D/dalvikvm(300): GC_CONCURRENT freed 528K, 16% free 21541K/25351K, paused 15ms+7ms, total 89ms
07-26 20:45:28.204: W/BufferQueue(124): [com.android.launcher/com.android.launcher2.Launcher] cancelBuffer: BufferQueue has been abandoned!
07-26 20:45:28.228: V/KeyguardViewManager(300): KGVM: Set visibility on com.android.internal.policy.impl.KeyguardViewManager$KeyguardViewHost@41d2ef48 to 6291456
07-26 20:45:28.236: D/PhoneStatusBar(399): disable: < expand icons alerts ticker system_info back home RECENT* clock >
07-26 20:45:28.243: I/Choreographer(300): Skipped 35 frames!  The application may be doing too much work on its main thread.
07-26 20:45:28.329: V/LockPatternKeyguardView(300): Set visibility on com.android.internal.policy.impl.LockPatternKeyguardView$4@41e57948 to 8388608
07-26 20:45:28.329: D/PhoneStatusBar(399): disable: < expand icons alerts ticker system_info BACK* HOME* RECENT CLOCK* >
07-26 20:45:28.407: V/LockPatternKeyguardView(300): Set visibility on com.android.internal.policy.impl.LockPatternKeyguardView$4@41e57948 to 8388608
07-26 20:45:28.423: V/TransportControlView(300): Create TCV com.android.internal.widget.TransportControlView@41e953f8
07-26 20:45:28.431: I/WindowManager(300): Lock screen displayed!
07-26 20:45:28.446: V/LockPatternKeyguardView(300): Set visibility on com.android.internal.policy.impl.LockPatternKeyguardView$4@41e57948 to 8388608
07-26 20:45:28.501: V/LockPatternKeyguardView(300): Set visibility on com.android.internal.policy.impl.LockPatternKeyguardView$4@41e57948 to 8388608
07-26 20:45:28.704: D/SurfaceFlinger(124): Screen about to return, flinger = 0x41b48af0
07-26 20:45:28.704: E/PowerManagerService(300): Excessive delay setting brightness: 271ms, mask=2

Please share any ideas...

Was it helpful?

Solution

The only reasonable solution I could find was to use handlers

            Handler handlerUI = new Handler();
            handlerUI.postDelayed(new Runnable() {
                @Override
                public void run() {
                    deviceManger.lockNow();
                }
            }, 500);
            Handler handlerUI2 = new Handler();
            handlerUI2.postDelayed(new Runnable() {
                @Override
                public void run() {
                    deviceManger.lockNow();
                }
            }, 900);

I haven't experienced any screen flashes since I use this handlers, and I am using the app daily. You can find the app on market https://play.google.com/store/apps/details?id=com.alinberce.standbytouch

OTHER TIPS

You should first lock the device and then finish activity...

instead of this code:

 finish();
 deviceManger.lockNow();

use this :

 deviceManger.lockNow();
 finish();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top