KeyguardManager FLAG_DISMISS_KEYGUARD的服务 [英] KeyguardManager FLAG_DISMISS_KEYGUARD for Service

查看:140
本文介绍了KeyguardManager FLAG_DISMISS_KEYGUARD的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当屏幕开启时,我要检查的电源按钮来激活它,如果是,它会自动关闭该键盘保护和运行敬酒。

当屏幕被关闭时,键盘锁将被重新启用。 (在code工作到这里)

然而,当屏幕关闭,我pressed音量按钮时,屏幕打开。 (但它进入其中在检测到环路电源按钮是pressed)。

应当将案件不得激活辞退键盘锁,当其他按钮(除电源按钮)是pressed。

任何帮助是pciated解决这个bug太多AP $ P $:)

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

 公共类为myService延伸服务{

@覆盖
公众的IBinder onBind(意向为arg0){
    // TODO自动生成方法存根
    返回null;
}

@覆盖
公共无效的onCreate(){
    super.onCreate();

}

@覆盖
公共无效ONSTART(意向意图,诠释startId){
    布尔screenOn = intent.getBooleanExtra(screen_state,假);
    布尔pressed = FALSE;
    振动器myVib;

    //屏幕开启
    如果(!screenOn)
    {
        pressed的onkeydown =(26,NULL);

        //如果开启的电源按钮。错误=总是进入这个循环
        如果(pressed)
        {
            上下文的背景下= getApplicationContext();
            CharSequence的文字=服务启动电源按钮pressed。
            INT持续时间= Toast.LENGTH_SHORT;

            吐司面包= Toast.makeText(背景,文本,持续时间);
            toast.show();

            // dimiss键盘保护
            KeyguardManager keyguardManager =(KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
            KeyguardLock锁= keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
            lock.disableKeyguard();


            上下文context2 = getApplicationContext();
            CharSequence的文本2 =屏幕上;
            INT duration2 = Toast.LENGTH_SHORT;

            吐司烤面包= Toast.makeText(context2,文本2,duration2);
            toast2.show();

        }
        其他
        {
            上下文的背景下= getApplicationContext();
            CharSequence的文字=服务开始没电源按钮pressed。
            INT持续时间= Toast.LENGTH_SHORT;

            吐司面包= Toast.makeText(背景,文本,持续时间);
            toast.show();
        }

    }

    //屏幕被关闭
    其他 {
        myVib =(震动)this.getSystemService(VIBRATOR_SERVICE);
        myVib.vibrate(500);

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

    }
}

 公共布尔的onkeydown(INT键code,KeyEvent的事件){
     如果(键code == KeyEvent.KEY code_POWER)
         返回true;
     其他
        返回false;
 }
为myService类} //结束
 

解决方案

好了,所以这不是简单,因为它似乎 服务不能真正拥有当前正在运行的活动,但你需要一个活动来获得一个参考窗口

所以,你可以做到这一点了几个不同的方式。

在这个问题给出的答案虚活动创建 (一个未充气),并用来获取窗口(工程,但 不知道是什么,如果未充气的活动导致泄漏或其他问题。再加上它看起来有点像一个肮脏的黑客)

或者......

的CommonWares方式

<一href="http://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service">how我可以得到当前的前景活动从服务
你可以处理实际的操作窗口中轰出了回调,当你的服务广播的意图 到活动将这样做。 (更好的设计,但多一点archetectural改造)

一旦你决定要如何做(我会建议#2) 下面应该工作,但是,请注意我没有测试过。

  //从上下文窗口
窗口管理WM = Context.getSystemService(Context.WINDOW_SERVICE);

//开锁
//http://developer.android.com/reference/android/app/Activity.html#getWindow()
窗口窗口= getWindow();
window.addFlags(wm.LayoutParams.FLAG_DISMISS_KEYGUARD);

//锁定装置
DevicePolicyManager mDPM;
mDPM =(DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);  

When the screen is turned on, I want to check is power button activated it, if yes, it will auto dismiss the keyguard and run the toast.

When the screen is turned off, the keyguard will be re-enabled. (the code works till here)

However, when the screen is OFF, and i pressed "Volume up" button, the screen turns ON. (but it goes into the loop where it detects "power" button is pressed).

The case should be it shall not activate "Dismiss keyguard" when other buttons(except "power" button) are pressed.

any help is much appreciated to solve this bug :)

another question - how do I use FLAG_DISMISS_KEYGUARD in Service?

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

解决方案

Okay so this is not as simple as it would seem Services cannot actually own the currently running activity but you need an activity to get a reference to the window

So you could do that bit a couple of different ways.

Turning off the screen from a service
In the answer given in this question a dummy activity is created (one that is not inflated) and used to get the window (works but not sure what if the uninflated activity causes a leak or other issues. Plus it seems kinda like a dirty hack)

Or.....

The CommonWares way

how can i get the current foreground activity from a service
You could handle the actual window manipulation in a callback fired when your service broadcasts an intent to the activity that will be doing this. (Better design but a bit more archetectural reworking) Example

Once you decide how you want to do (I would recommend #2) The following should work but, note I have not tested it.

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

这篇关于KeyguardManager FLAG_DISMISS_KEYGUARD的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆