簡體   English   中英

如何在android中單擊通知操作時要求用戶解鎖設備?

[英]How to ask user to unlock device on clicking notification action in android?


我正在顯示來自我的應用程序的通知,並且此通知中有一個操作,當用戶單擊該操作時,將使用我設置的意圖調用相應的操作類。 現在,我想執行一個特定的操作,但在此之前,用戶需要解鎖屏幕,如果它是針腳/圖案保護。 我無法要求用戶解鎖設備,即在鎖定屏幕上打開解鎖鍵盤/圖案。
以下是我的代碼,

    //HandleAction is a java class that extends IntentService
    Intent intent = new Intent(context, HandleAction.class);
    intent.putExtra(key, "my_value"); //Used to send information to action class
    PendingIntent pi = PendingIntent.getService(context, 0, intent,
                                 PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext);
    //set the title, icon etc for builder and add action as below
    notification.addAction(icon, "my_label", pi);

當用戶單擊通知操作時,我將控件發送到MyAction.java中的onHandleIntent
在這里,我想要求用戶在密碼保護的情況下解鎖設備,然后執行操作。 如何在onHandleIntent中請求用戶解鎖設備?

我發現使用KeyguardManager和KeyguardLock來實現這一點,但是keyguardManager.newKeyguardLock是不推薦使用的方法,我想避免這種情況。 所以,下一個是使用“FLAG_TURN_SCREEN_ON”和“FLAG_KEEP_SCREEN_ON”,但我無法弄清楚如何在這種情況下使用它們。 我沒有從我的動作類中啟動任何窗口,它只是一個增加我的計數器的操作。 點擊它后,通知應該消失,執行我的操作就可以了。

我發現了一個類似的問題解鎖手機 ,但它的方式是通過啟動虛擬/空活動。

在此先感謝任何幫助,建議:)

在服務的待處理意圖中使用活動意圖

Intent intent = new Intent(context, MyActivity.class);
intent.putExtra(key, "my_value"); //Used to send information to action class
PendingIntent pi = PendingIntent.getActivity(context, 0, intent,
                                 PendingIntent.FLAG_UPDATE_CURRENT);

在您打開的活動中(位於鎖定屏幕后面),您應告訴系統抬起鍵盤鎖(例如在onCreate()

對於API> = 26

KeyguardManager km = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
km.requestDismissKeyguard(this, null); // you may add callback listener here

對於API <26:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

如果設備沒有安全鎖定,它會立即解鎖,如果它被鎖定,它會要求用戶這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM