屏幕打開時(shí),我要檢查電源按鈕是否被激活,如果是,它將自動關(guān)閉鍵盤鎖并運(yùn)行吐司.
屏幕關(guān)閉時(shí),將重新啟用鍵盤鎖. (代碼在這里起作用)
但是,當(dāng)屏幕關(guān)閉時(shí),我按下“調(diào)高音量”按鈕,屏幕打開. (但是進(jìn)入檢測到“電源”按鈕被按下的循環(huán)).
這種情況應(yīng)該是當(dāng)按下其他按鈕(“電源”按鈕除外)時(shí),它不能激活“關(guān)閉鍵盤鎖”.
非常感謝您提供任何幫助來解決此錯(cuò)誤:)
另一個(gè)問題-如何在服務(wù)中使用FLAG_DISMISS_KEYGUARD?
public class myService extends Service{@Overridepublic IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null;}@Overridepublic void onCreate() { super.onCreate();}@Overridepublic 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
解決方法:
好吧,這并不像看起來那樣簡單
服務(wù)實(shí)際上不能擁有當(dāng)前正在運(yùn)行的活動,但是您需要一個(gè)活動來獲取對該窗口的引用
因此,您可以通過幾種不同的方式來做到這一點(diǎn).
Turning off the screen from a service
在此問題給出的答案中,將創(chuàng)建一個(gè)虛擬活動
(未充氣的)(用來充氣)(可以但
不知道未充氣的活動會導(dǎo)致泄漏或其他問題怎么辦.另外,似乎有點(diǎn)像骯臟的駭客)
要么…..
CommonWares方式
how can i get the current foreground activity from a service
您可以在服務(wù)廣播意圖時(shí)觸發(fā)的回調(diào)中處理實(shí)際的窗口操作
進(jìn)行此操作的活動. (更好的設(shè)計(jì),但更多的是架構(gòu)上的修改)
Example
一旦您決定要怎么做(我會建議#2)
以下應(yīng)該可以工作,但是請注意,我尚未對其進(jìn)行測試.
//Get the window from the contextWindowManager 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 deviceDevicePolicyManager mDPM;mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
來源:https://www.icode9.com/content-4-587851.html