How to keep a foreground app running 24/7?

前端 未结 7 717
时光说笑
时光说笑 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 19:01

    I solved that issue by having a sticky service running that relaunches the app when the activity is getting closed.

    //Your activity
    
    @Override
    public void onPause() {
        super.onPause();
    
        if (yourservice != null) {
            yourservice.validateActivityOnPause();
        }
    }
    

    and in the validateActivityOnPause() have something like:

    //Your service
    
    public void validateLynxActivityOnPause() {
        //Do some stuff here
    
        Intent startActivityIntent = new Intent(this, LynxActivity.class);
        startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(startActivityIntent);
    }
    

提交回复
热议问题