Вопрос

I created an android app. The screen never turns off:

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

Now I want to show a picture if the screen is not pressed for 5 minutes or something else. The app should not be closed, when pressing on the image the app should be open.

How can I realize that?

Это было полезно?

Решение

I would discourage you from taking this approach. Users expect to have a consistent user experience between various apps on their devices, and likely have a preference to how their device sleeps, either by having specified a sleep timeout or displaying a daydream as introduced in Android 4.2.

If you'd like to provide users with the option to display a screensaver associated with your app, I suggest including a Daydream in your app and otherwise acknowledging the user's preferences.

That being said, if you cannot use Daydream, you could observe if the app is being used or not. Two things come to mind:

  1. Have the root view of your activity intercept touch events to observe if any of its children have been touched.
  2. Observe the activity's onPause() and onResume() to acknowledge that the activity is still being displayed.

You could then invoke a Runnable by posting it to a view using postDelayed(Runnable action, long delayMillis), being wary to remove it when the activity is paused or the timer should be reset using removeCallbacks(Runnable).

Другие советы

I solved the problem!!!

I used that event:

    public boolean dispatchTouchEvent(MotionEvent ev) 
    {

        super.dispatchTouchEvent(ev);

        // cancel my Timer

        return true;

    }

Thanks!!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top