其他分享
首页 > 其他分享> > android – 解锁设备,显示文本,然后再次锁定

android – 解锁设备,显示文本,然后再次锁定

作者:互联网

对于我的应用程序的需要,我需要在屏幕上显示一条消息,即使启用了锁屏,然后等待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);
    }
}

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

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

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

我认为我的用户无法理解设备管理员,也无法激活它.是否有任何解决方法在没有设备管理员的情况下锁定屏幕?

解决方法:

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

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

标签:android,lockscreen,device-admin
来源: https://codeday.me/bug/20190629/1330146.html