How to keep a foreground app running 24/7?

前端 未结 7 620
时光说笑
时光说笑 2021-01-30 18:25

I am looking into how to keep my Android app running in the foreground.

It will be a privately distributed app, so I can do anything possible to make sure it runs consta

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-30 18:57

    use this:

    import android.os.PowerManager;
    
    public class MyActivity extends Activity {
    
        protected PowerManager.WakeLock mWakeLock;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(final Bundle icicle) {
            setContentView(R.layout.main);
    
            /* This code together with the one in onDestroy() 
             * will make the screen be always on until this Activity gets destroyed. */
            final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
            this.mWakeLock.acquire();
        }
    
        @Override
        public void onDestroy() {
            this.mWakeLock.release();
            super.onDestroy();
        }
    }
    

    And in the manifest:

    
    

    Seen here: How do I keep the screen on in my App?

提交回复
热议问题