繁体   English   中英

Android活动解锁屏幕

[英]Android activity unlocking screen

我正在尝试添加一个Activity ,使锁定的屏幕解锁并显示一个Activity 除解锁屏幕外,其他所有操作均有效。 Activity开始,但屏幕保持关闭。 但是我需要屏幕在Activity开始的同时进行。

我正在使用以下代码:

public void vibrate() {
    if ((deltaX > vibrateThreshold) || (deltaY > vibrateThreshold) || (deltaZ > vibrateThreshold)) {

            //http://www.androidauthority.com/android-7-0-features-673002/

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("My notification")
                            .setContentText("Hello World!")
                            .setPriority(Notification.PRIORITY_MAX);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


            notificationManager.notify(0, mBuilder.build());

            Intent alarmIntent = new Intent("android.intent.action.MAIN");
            alarmIntent.setClass(this, test.class);
            alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        alarmIntent.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            this.startActivity(alarmIntent);

            new CountDownTimer(25000, 1000) {

                public void onTick(long millisUntilFinished) {
                    //here you can have your logic to set text to edittext
                }
            public void onFinish() {
            }
            }.start();
    }
}

在我的AndroidManifest中,我有以下代码:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

如何解锁和锁定屏幕的代码段:

//Get the window from the context    
WindowManager wm = Context.getSystemService(Context.WINDOW_SERVICE);   

//Unlock
Window window = getWindow();  
window.addFlags(wm.LayoutParams.FLAG_DISMISS_KEYGUARD);  

//Lock device  
DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);

尝试使用KeyguardManager

例:

KeyguardManager.KeyguardLock keyguardLock = keyguardLock = ((KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE)).newKeyguardLock("TAG");

keyguardLock.disableKeyguard(); // to unlock the device
keyguardLock.reenableKeyguard(); // to lock the device, only works if your application called .disableKeyguard before

需要的权限:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

唤醒设备:

// Called from onCreate
protected void createWakeLocks(){
    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK");
    partialWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Loneworker - PARTIAL WAKE LOCK");
}

// Called implicitly when device is about to sleep or application is backgrounded
protected void onPause(){
    super.onPause();
    partialWakeLock.acquire();
}

// Called implicitly when device is about to wake up or foregrounded
protected void onResume(){
    super.onResume();
    if(fullWakeLock.isHeld()){
        fullWakeLock.release();
    }
    if(partialWakeLock.isHeld()){
        partialWakeLock.release();
    }
}

来源: Android-唤醒和解锁设备
另一个链接: Android:唤醒和解锁手机

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM