Longer screen timeout for specific Android activity?

We have similar posts, but none of them are what I'm looking for. I would like to change the screen timeout for a specific action. I want to avoid using WakeLock, if possible, and I do not want to change the system timeout delay setting.

Is there a way to do this without manually tracking user activity and using tracking locks?

= ---- =

Clarification: for example, how can you set the timeout for screen inactivity (the time it takes to turn off the screen after there is no input), some value, for example, 3 minutes?

This can be done by setting the system parameters, but it affects the entire device (even after closing the application), so this is not a good solution.

Thanks!

+7
android
source share
4 answers

First, pay attention to changing system settings to increase the latency for your activity. Intuitively, I do not like this approach, since you are making a system-wide change just to host your application. Moreover, when changing the system settings in this way, it is difficult to guarantee that you can install it back (that is, you forgot to install it back or the application crashes before you install it).

Brief description of the solution:

  • Use the Activity.onUserInteraction () method for Activity, which is called for key, touch, trackball events.
  • When in the root view mode of your activity there is a user interaction call View.setKeepScreenOn () (or some view in your activity that is constant).
  • Use a simple Handler to send delayed messages to turn off screen saving after a certain time.

Now for the code:

private ViewGroup mActivityTopLevelView; private static final int DISABLE_KEEP_SCREEN_ON = 0; private static final int SCREEN_ON_TIME_MS = 1000*60*3; @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume"); setScreenOn(true); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); setScreenOn(false); } @Override public void onUserInteraction() { super.onUserInteraction(); Log.d(TAG, "onUserInteraction"); setScreenOn(true); } private void setScreenOn(boolean enabled) { // Remove any previous delayed messages Log.d(TAG, "setScreenOn to " + enabled); mHandler.removeMessages(DISABLE_KEEP_SCREEN_ON); if( enabled ) { // Send a new delayed message to disable the screen on // NOTE: After we call setKeepScreenOn(false) the screen will still stay on for // the system SCREEN_OFF_TIMEOUT. Thus, we subtract it out from our desired time. int systemScreenTimeout = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 0); int totalDelay = SCREEN_ON_TIME_MS - systemScreenTimeout; if( totalDelay > 0 ) { mActivityTopLevelView.setKeepScreenOn(true); Log.d(TAG, "Send delayed msg DISABLE_KEEP_SCREEN_ON with delay " + totalDelay); mHandler.sendEmptyMessageDelayed(DISABLE_KEEP_SCREEN_ON, totalDelay); } } else { mActivityTopLevelView.setKeepScreenOn(false); } } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { if(msg.what == DISABLE_KEEP_SCREEN_ON) { setScreenOn(false); } } }; 

Tested on Android 5.1.0.

+4
source share

From what I read, it looks like you are trying to fire an activity after a while. (Please correct me if I misinterpreted your question.) You can try using a timer to fire the action after a certain time. For example:

 new Timer().schedule(task, timeout); 

Where the task relates to the method that should be expelled (a function that rejects your activity) and the timeout represents the delay before the task is completed.

0
source share

you have 2 requirements from what I understand in your question,
1) change the timeout of the screen of a specific event.
2) if the user does not interact for a long screen lock. You can do this to change the timeout of the screen , since you did not want to use WAKE LOCK, try this

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } 

you can read about it here https://developer.android.com/training/scheduling/wakelock.html

For the second part
As soon as the application starts the timer in the general preference of the application, and if the number of timers crosses the required time interval, then reset the flag so that the screen locks

0
source share

Just temporarily change the setting when your application is in the foreground:

 String originalTimeout; int temporaryTimeout = 1 * 60 * 1000; @Override protected void onResume() { super.onResume(); originalTimeout = Settings.System.getString(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT); Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, temporaryTimeout); } @Override protected void onPause() { super.onPause(); Settings.System.putString(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, originalTimeout); } 

This requires the permission android.permission.WRITE_SETTINGS .

0
source share

All Articles