令人沮丧的DevicePolicyManager lockNow()行为

时间:2012-07-26 17:47:11

标签: android lockscreen standby device-manager

事情很简单:按一个图标并将手机发送到待机状态。令人沮丧的部分来自手机:有时,随机而不是保持待机,屏幕唤醒并显示锁定屏幕。我还没有找到任何条件来触发这种行为,大概是3-5次尝试中的一次。该应用程序具有管理员权限,好奇地在自定义ROM工作正常,但库存JellyBean 4.1.1没有。

    @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();
      }
   }

我有我的xml政策文件,如

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

Android清单似乎没问题

<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>

问题是我无法弄清楚为什么会这样。这是发生在Galaxy Nexus上的库存,干净安装4.1.1正如您所看到的,代码很简单。 我可以尝试使用post延迟500毫秒制作一个处理程序并再次运行lockNow()但是如果在某些设备上500毫秒还不够呢?

当出现锁定屏幕时,logcat具有:

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

请分享任何想法......

2 个答案:

答案 0 :(得分:1)

我能找到的唯一合理的解决方案是使用处理程序

            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);

由于我使用此处理程序,因此我没有遇到任何屏幕闪烁,而且我每天都在使用该应用程序。您可以在市场上找到该应用https://play.google.com/store/apps/details?id=com.alinberce.standbytouch

答案 1 :(得分:0)

您应首先锁定设备,然后完成活动...

代替此代码:

 finish();
 deviceManger.lockNow();

使用此:

 deviceManger.lockNow();
 finish();