4

为了我的应用程序的需要,即使启用了锁屏,我也需要在屏幕上显示一条消息,然后等待 3 秒钟,然后我必须再次锁定手机,因为我不希望它在您的手机中拨打不需要的电话口袋。

第一部分很简单:

if (PreferenceManager.getDefaultSharedPreferences(
    getBaseContext()).getBoolean("wake", false)) {
    KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
    WakeLocker.acquire(ProtoBenService.this);
    Intent myIntent = new Intent(ProtoBenService.this,LockActivity.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    if (isKeyguardUp) {
        ProtoBenService.this.startActivity(myIntent);
    } else
    Toast.makeText(ProtoBenService.this.getBaseContext(), intention, Toast.LENGTH_LONG).show();

    WakeLocker.release();
}

有了这个类:

public abstract class WakeLocker {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context ctx) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "CobeIm");
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }
}

和活动:

public class LockActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        TextView tv = new TextView(this);
        tv.setText("This is working!");
        tv.setTextSize(45);
        setContentView(tv);

        Runnable mRunnable;
        Handler mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {
                LockActivity.this.finish();
            }
        };
        mHandler.postDelayed(mRunnable, 3 * 1000);
    }
}

所以,这很好,手机可以显示我的文字!

唯一的问题是当我想再次锁定手机时,似乎锁定手机受到系统保护......

我认为我的用户不会理解设备管理员并且无法激活它。是否有任何解决方法可以在没有设备管理员的情况下锁定屏幕?

4

2 回答 2

2

我使用以下方法锁定和解锁手机。

初始化

        KeyguardLock keyguard;
        KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        keyguard = km.newKeyguardLock("MyApp");

解锁手机

keyguard.disableKeyguard();

再次锁定手机

keyguard.reenableKeyguard();    

打开屏幕

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

并且不要忘记使用以下权限

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
于 2012-12-06T02:34:34.807 回答
1

我很确定您必须使用设备管理功能来锁定屏幕。

    protected static void initiateDeviceLock(Context context) {
    ComponentName componentName = new ComponentName(context, MyDeviceAdminReceiver.class);
    DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
    boolean active = dpm.isAdminActive(componentName);
    Log.i(context.getClass().getSimpleName(), "Active (in initiateDeviceLock) = " + String.valueOf(active));
    if (active) {
        dpm.lockNow();
    }
}

为了帮助用户设置设备管理功能,您可以将他们带到设置页面:

    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    ComponentName componentName = new ComponentName(TestActivity.this, MyDeviceAdminReceiver.class);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
    startActivityForResult(intent, CODE);
于 2012-12-05T23:02:01.857 回答