Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
70 views
in Technique[技术] by (71.8m points)

java - Keep Activity Running

I am developing an app specifically for the Nvidia Shield that we prepare and ship to people. My requirement is to have the app always running since it is a visual display (and not expected to be used for any other purpose). And since it is expected always to be plugged in there is no need for concern of battery life.

I've tried both an internal (to the app) and an external app service running to keep the app alive, by trying to restart it if for some reason it is no longer running. But, after a few days both the external service and the main app seem to be killed off, as the Thread.setDefaultUncaughtExceptionHandler is not catching any crashes to log.

The service does restart the app after any force close, and the setDefaultUncaughtExceptionHandler does restart the app after a crash, but I assume the OS is killing the app. I'd assume a light weight external service would be able to stay alive.

I am trying to avoid rooting the device out of concern of performance issues, loss of needed features and missing out on stability updates.

I am out of ideas to work around this issue and having to wait days to see if it works or not is wasting too much time for development.

However, I haven't tried it with turning the energy optimization options turned off for either app. I'm not sure if, or how much, of an effect that would have.

Targeting SDK 30

the manifest:

<service
        android:name=".RestartService"
        android:enabled="true" />

Main activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);Thread.setDefaultUncaughtExceptionHandler(
            new CrashHandler(
                    this,
                    MainActivity.class
            )
    );

    if(!isServiceRunning()) {
        startService(new Intent(this, RestartService.class));
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
}

private boolean isServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (RestartService.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }

    return RestartService.isRunning();
}

the Restart Service:

private static boolean isRunning;

public RestartService() { }

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    isRunning = true;

    initRestartInterval();

    return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {
    isRunning = false;
}

public static boolean isRunning() {
    return isRunning;
}

public static void setRunning(boolean newState) {
    isRunning = newState;
}

private void initRestartInterval() {
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.company.playerapp");
            if (launchIntent != null) {
                launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Logger.out("Attempting to start activity");
                startActivity(launchIntent);
            }
        }
    };
    timer.schedule(task, 0, 60000);// In ms 60 secs is 60000
}
question from:https://stackoverflow.com/questions/65887982/keep-activity-running

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...