日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

android-KeyguardManager FLAG_DISMISS_KEYGUARD服務(wù)

 印度阿三17 2019-12-08

屏幕打開時(shí),我要檢查電源按鈕是否被激活,如果是,它將自動(dòng)關(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{

@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

解決方法:

好吧,這并不像看起來那樣簡單
服務(wù)實(shí)際上不能擁有當(dāng)前正在運(yùn)行的活動(dòng),但是您需要一個(gè)活動(dòng)來獲取對該窗口的引用

因此,您可以通過幾種不同的方式來做到這一點(diǎn).

Turning off the screen from a service
在此問題給出的答案中,將創(chuàng)建一個(gè)虛擬活動(dòng)
(未充氣的)(用來充氣)(可以但
不知道未充氣的活動(dòng)會(huì)導(dǎo)致泄漏或其他問題怎么辦.另外,似乎有點(diǎn)像骯臟的駭客)

要么…..

CommonWares方式
how can i get the current foreground activity from a service
您可以在服務(wù)廣播意圖時(shí)觸發(fā)的回調(diào)中處理實(shí)際的窗口操作
進(jìn)行此操作的活動(dòng). (更好的設(shè)計(jì),但更多的是架構(gòu)上的修改)
Example

一旦您決定要怎么做(我會(huì)建議#2)
以下應(yīng)該可以工作,但是請注意,我尚未對其進(jìn)行測試.

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

//Unlock
//http://developer./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);
來源:https://www./content-4-587851.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多