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