简体   繁体   English

android:当应用程序在后台时调暗屏幕

[英]android: dim the screen when application is in background

I am developing a small app which shows passwords of the user through a Dialog screen. 我正在开发一个小型应用程序,该应用程序通过“对话”屏幕显示用户密码。

When home button is pressed, I need to dim the screen (on the multi tasking window) so that any other person cannot see the password. 当按下主页按钮时,我需要使屏幕变暗(在多任务处理窗口上),以便其他任何人都看不到密码。

When user re-opens the app, it asks an application lock. 当用户重新打开应用程序时,它会要求应用程序锁定。 But if the user leaves the password Dialog open and presses the home button, dialog and the password which user last looked at stays visible (on the multi tasking window) for a while (3-4 seconds!!) until a new dialog asks the lock. 但是,如果用户将密码对话框保持打开状态并按下主屏幕按钮,对话框,则用户上次查看的密码(在多任务处理窗口上)将保持可见状态(3-4秒钟!),直到出现新对话框询问锁。

So far I tried ever possible dialog.dissmiss() options. 到目前为止,我尝试了所有可能的dialog.dissmiss()选项。 Dialog dismisses only when app is opened again (until a new lock dialog appears) even I put dismiss() in onPause, onStop etc. 对话框仅在再次打开应用程序时关闭(直到出现新的锁定对话框),即使我将dismiss()放在onPause,onStop等中也是如此。

Any idea appreciated. 任何想法表示赞赏。

I also tried, 我也尝试过

android.os.Process.killProcess(android.os.Process.myPid());
this.finish();
System.exit(0);

none of them actually worked. 他们都没有实际工作。

Suggestion 1: Double-check your implementation. 建议1:仔细检查您的实施。 Tying your dialog to the activity lifecycle seems like a good idea (especially to avoid leaked window errors as described here ) 将对话框绑定到活动生命周期似乎是个好主意(尤其是避免出现此处所述的泄漏窗口错误)

The following example works out well for me (with coachMark being derived from Dialog) 以下示例对我来说很不错(coachMark源自Dialog)

@Override
protected void onResume()
{
    log.debug("onResume");
    super.onResume();

    // Show the coachMark depending on saved preference values
    coachMark.mayBeShow();
}

@Override
protected void onPause()
{
    log.debug("onPause");

    // Hide the coachMark if it is showing to avoid leakedWindow errors
    coachMark.maybeHide();

    super.onPause();
}

onPause definately gets called when you press the home button, so if this approach does not work for you, try not recreating the dialog in the restarting part of the acitivty lifecycle (onRestart(), onStart() and onResume()) and see, if it gets dismissed correctly. 当您按下主页按钮时,肯定会调用onPause ,因此,如果这种方法不适合您,请尝试不要在活动生命周期的重启部分(onRestart(),onStart()和onResume())中重新创建对话框,然后看看,如果它被正确解雇了。


Suggestion 2: Should all of the above fail, you might consider overriding the home button as described here . 建议2:如果以上所有操作均失败,则可以考虑按此处所述覆盖“主页”按钮。 I highly advise against it though, since this may cause the app to work in an way that the user does not expect it to. 不过,我强烈建议您这样做,因为这可能会导致应用程序无法以用户期望的方式运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM