其他分享
首页 > 其他分享> > android-KeyguardManager FLAG_DISMISS_KEYGUARD服务

android-KeyguardManager FLAG_DISMISS_KEYGUARD服务

作者:互联网

屏幕打开时,我要检查电源按钮是否被激活,如果是,它将自动关闭键盘锁并运行吐司.

屏幕关闭时,将重新启用键盘锁. (代码在这里起作用)

但是,当屏幕关闭时,我按下“调高音量”按钮,屏幕打开. (但是进入检测到“电源”按钮被按下的循环).

这种情况应该是当按下其他按钮(“电源”按钮除外)时,它不能激活“关闭键盘锁”.

非常感谢您提供任何帮助来解决此错误:)

另一个问题-如何在服务中使用FLAG_DISMISS_KEYGUARD?

public class myService extends Service{

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

}

@Override
public void onStart(Intent intent, int startId) {
    boolean screenOn = intent.getBooleanExtra("screen_state", false);
    boolean pressed = false;       
    Vibrator myVib;

    //screen is turned on
    if (!screenOn) 
    {
        pressed = onKeyDown(26, null);

        //if it turned on by power button. bug = always go into this loop
        if(pressed)
        {
            Context context = getApplicationContext();
            CharSequence text = "Service started. power button pressed";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();               

            //dimiss keyguard
            KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
            KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
            lock.disableKeyguard();


            Context context2 = getApplicationContext();
            CharSequence text2 = "SCREEN ON";
            int duration2 = Toast.LENGTH_SHORT;

            Toast toast2 = Toast.makeText(context2, text2, duration2);
            toast2.show();

        }           
        else
        {
            Context context = getApplicationContext();
            CharSequence text = "Service started. NOT power button pressed";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }

    }

    //screen is turned off
    else {
        myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
        myVib.vibrate(500);

        KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
        KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
        lock.reenableKeyguard();

    }
}

 public boolean onKeyDown(int keyCode, KeyEvent event) {
     if(keyCode == KeyEvent.KEYCODE_POWER)
         return true;
     else
        return false;
 }
}//end of myService class

解决方法:

好吧,这并不像看起来那样简单
服务实际上不能拥有当前正在运行的活动,但是您需要一个活动来获取对该窗口的引用

因此,您可以通过几种不同的方式来做到这一点.

Turning off the screen from a service
在此问题给出的答案中,将创建一个虚拟活动
(未充气的)(用来充气)(可以但
不知道未充气的活动会导致泄漏或其他问题怎么办.另外,似乎有点像肮脏的骇客)

要么…..

CommonWares方式
how can i get the current foreground activity from a service
您可以在服务广播意图时触发的回调中处理实际的窗口操作
进行此操作的活动. (更好的设计,但更多的是架构上的修改)
Example

一旦您决定要怎么做(我会建议#2)
以下应该可以工作,但是请注意,我尚未对其进行测试.

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

//Unlock
//http://developer.android.com/reference/android/app/Activity.html#getWindow()
Window window = getWindow();  
window.addFlags(wm.LayoutParams.FLAG_DISMISS_KEYGUARD);  

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

标签:android-emulator,android,service
来源: https://codeday.me/bug/20191207/2087375.html