本文介绍了解锁设备,显示文本,然后再次锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用程序的需要,即使启用了锁定屏幕,我也需要在屏幕上显示一条消息,然后等待3秒钟,然后我不得不再次锁定手机,因为我不想让它成为不需要的手机

For the need of my application, I need to display a message on the screen even if the lockscreen is enabled, then wait 3 seconds, than I have to lock again the phone as I don't want it to make unwanted phone calls in your pockets.

第一部分很简单:

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

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

So, this is nice, the phone can display my text!

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

The only problem comes when I want to lock again the phone, it seems that locking the phone is protected by the system...




  • Programmatically turning off the screen and locking the phone
  • how to lock the android programatically

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

I think that my users won't understand the Device Admin and won't be able to activate it. Is there any workaround to lock the screen without the Device Admin stuff?

推荐答案

我很确定您必须使用Device管理员功能可锁定屏幕。

I am pretty sure that you have to use the Device Admin Features to lock the screen.

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

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

To help the user's setup the Device Admin features you can take them to the settings page:

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

这篇关于解锁设备,显示文本,然后再次锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:40