0

I am using Android 7.1 SDK 25 for industry. I am want to create an application that can start with device and auto start when app crash, or any problems make my app turn off.

  • I am done with start app when device turn on

  • My problem : How to auto make my app always alive. I am not sure my app can running through every day, every week with out any bugs, crash... I can not waiting to restart device to turn on my app again. I find method make my app restart: `

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
int mPendingIntentId = 102;
PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), mPendingIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);

` But It is only work if my app work exactly as lifecycle , I placed it in onDestroy or somewhere I catch the bug. But it is not work if my app suddenly crash with out my control. I found this answer " keep a foreground app running 24/7" seem like my road

If you want to do this programmatically you can use a service that polls every "x" milliseconds to see if your app is in the foreground. If it is not, it will start/bring your app in the foreground.

But it is not work and no answer accepted in that topic.

UPDATE:*

As link answer above I am created a service, the service work since first time start app, and It is still work if my app killed. But I can call start app again

1st: I used "android.arch.lifecycle:extensions:1.1.0" to check app working on forceground or not.

public class ArchLifecycleApp extends Application implements LifecycleObserver {

@Override
public void onCreate() {
    super.onCreate();
    ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onAppBackgrounded() {
    //App in background
    Log.d("TAG", "App in background" );
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onAppForegrounded() {
    // App in foreground
    Log.d("TAG", "App in foreground" );
}

}

2nd: I created my service that checking app on froceground every 3s

public class PersistService extends Service {
private static final int INTERVAL = 3000; // poll every 3 secs

private WeakReference<MainActivity> weakReference = null;

public void init(MainActivity mainActivity){
    this.weakReference = new WeakReference<>(mainActivity);
    if (weakReference != null){
        MainActivity activity = weakReference.get();
        if (activity != null){
        }
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            Log.d("TAG", "start command");
            if (!ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)){
                weakReference.get().turnOnApp();//if app not in forceground then turn on it
            }
        }
    };
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(task, 0, INTERVAL);

    return super.onStartCommand(intent, flags, startId);
}

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

}

My turnOnApp in MainActivity

public void turnOnApp(){
    PackageManager pm = getPackageManager();
    Intent launchIntent = pm.getLaunchIntentForPackage("MY_PACKET_NAME");

    startActivity(launchIntent);
}

Service worked but can not call myapp turn back. It give Error

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.Reference.get()' on a null object reference

It happened Cause my app killed, main activity not running. I am confused that have other way to call app turn on

Jamebes
  • 96
  • 8
  • You should identify the device you are using as consumer devices (even with root) are subject to [Doze mode](https://developer.android.com/training/monitoring-device-state/doze-standby). Related: [Keep running background service (Android)](https://stackoverflow.com/a/64154826/295004) – Morrison Chang Nov 26 '22 at 08:28

1 Answers1

0

Are you looking to make a kiosk type app, where the user never leaves your app? If so, you want to make a home screen app, then set your app to be the default home screen. To make it the home screen, use:

<intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />

        <category android:name="android.intent.category.HOME" />

        <category android:name="android.intent.category.DEFAULT" />

    </intent-filter>

in your AndroidManifest as the intent filter for the Activity you want to be the home activity.

If not and you just want a constantly running app- can't be done on modern Android. It's purposely been made impossible, to preserve battery life. We might be able to help you do something else that will get you the data you need if you tell us more specifically what you're doing.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I am want a constantly running app. I am used Android 7.1(Android N) SDK 25 and I have root access. Battery is not my problem, It's ok. I am worry about my app not running cause any unknown reason. if happened I wanna make it auto turn on back – Jamebes Nov 26 '22 at 06:51
  • YOu can't have that. On Android any app that's not currently the foreground app can be killed at any time by the OS. There is no way to override that. If people find a way, Google kills it in the next version. So tell us what you're actually trying to do, and it might be there's a better architecture. – Gabe Sechan Nov 26 '22 at 07:11
  • FOr example the code you posted above used to work. Then to kill that workaround, Google decided that background processes can no longer launch foreground activities, except under very specific circumstances. – Gabe Sechan Nov 26 '22 at 07:13
  • bro, seem something wrong. Sr if my describe make complicated. My app look like normal app, turn on and keep his work. Only different is we have not interact's user. just auto turn on with the device and constantly running. But we know the app not always work like a charm, On nice day my app crashed and I can not restart device to my app wakeup. I update my code, can u look that and give me other advice – Jamebes Nov 26 '22 at 08:28