How do you keep your app from sleeping while it is running.

edited December 2013 in Android Mode

My tablet goes to sleep after one minute and I was just wondering how to force it to stay awake.

Answers

  • Answer ✓

    I have implemented similar functionality in my app GeoTecha. Please keep in mind that this kind of behavior can be potentially dangerous for the end user, draining the battery if they don't turn off the screen manually. Here's the code that I used:

    //Import WindowManager
    import android.view.WindowManager;
    
    //Override onCreate()
    @ Override
    public void onCreate(android.os.Bundle icicle) {
      super.onCreate(icicle);
    
      //This is the magic pixie dust
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
    
  • edited December 2013

    Thanks once again calsign. Also, how did you get the date when a high score was made in your app GeoTecha?

  • The value that is stored is:

    java.lang.System.currentTimeMillis()
    

    ...and it is converted into a readable format with:

    //Imports
    import java.text.SimpleDateFormat;
    import java.util.Locale;
    
    //Where "time" is the long value retrieved above
    SimpleDateFormat df = new SimpleDateFormat("d  MMM  y", Locale.US);
    String format = df.format(Long.parseLong(time);
    

    You can customize the format as per the rules in the SimpleDateFormat documentation.

Sign In or Register to comment.