gpt4 book ai didi

android - 用于服务的 KeyguardManager FLAG_DISMISS_KEYGUARD

转载 作者:太空狗 更新时间:2023-10-29 12:55:07 26 4
gpt4 key购买 nike

当屏幕打开时,我想检查电源按钮是否激活了它,如果是,它会自动关闭键盘锁并运行 toast。

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

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

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

非常感谢解决此错误的任何帮助:)

另一个问题 - 我如何在服务中使用 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

最佳答案

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

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

Turning off the screen from a service
在这个问题给出的答案中创建了一个虚拟 Activity (一个没有充气的)并用于获取窗口(有效但不确定未膨胀的 Activity 是否会导致泄漏或其他问题。另外它看起来有点像一个肮脏的黑客)

或者……

CommonWares 方式

how can i get the current foreground activity from a service
您可以在服务广播 Intent 时触发的回调中处理实际的窗口操作到将要执行此操作的 Activity 。 (更好的设计,但更多的架构改造) 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 - 用于服务的 KeyguardManager FLAG_DISMISS_KEYGUARD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7538923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com