1

I have a timer going on in the background service. It broadcasts an intent, which is received in activities onReceive() I want to start the application if it is killed by user. Else I want to continue using the application. I am using following code to restart the application from onReceive().

if(/*Application is killed*/) {
    Intent i = new Intent(context, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}

which condition should be used in if block?

Tushar Thakur
  • 956
  • 3
  • 15
  • 32
  • Do you want to start new MainActivity each and every time or want to launch MainActivity when application is killed.And If your application in already running then which activity you want to show? – VikasGoyal Jan 11 '16 at 05:56
  • Service will send a broadcast, at that moment if application is alive, I am performing some logic, else I want to start new MainActivity and perform same logic – Tushar Thakur Jan 11 '16 at 05:59
  • If my application is running then I want stay on same screen where I am, – Tushar Thakur Jan 11 '16 at 06:00
  • check [this post](http://stackoverflow.com/a/5862048/2553431) – Iamat8 Jan 11 '16 at 06:04

3 Answers3

2

You just need to check if you application is running or not in on receive

Code Snippet

boolean isApplicationRunning = false;
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++){
if(procInfos.get(i).processName.equals("com.me.checkprocess")){
    Log.e("Result", "App is running - Doesn't need to reload");
    isApplicationRunning = true;
    break;
 } 
 else
 {
    Log.e("Result", "App is not running - Needs to reload);
  }
}

and then you can execute

if(isApplicationRunning) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

}

  • The above solution works, but I have a background service with same process name, so even if the application is killed, the isApplicationRunning is true – Tushar Thakur Jan 11 '16 at 11:08
  • In that case you can keep one flag in you shared preference isApplication Running and whenever your first activity oncreate() executes set that flag to true and when ondestroy() gets called set it to false. Now you can check if sharepreference isApplicationRunning flag is false in you onreceive than perform your operation – Praween Kumar Mishra Jan 11 '16 at 14:36
0

To check whether you application is running or not what you can do is :-

if(isProcessRunning("package_name")) {
    Intent i = new Intent(context, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}


public boolean isProcessRunning(String process) {
    ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
    List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
    for(int i = 0; i < procInfos.size(); i++){
        if (procInfos.get(i).processName.equals(process)) {
            return true;
        }  
    }
    return false;
}

Try this it will works.

If your application is having only one or two activity or if your application have one BaseActivity which is extends by other then do it like:-

 public class MyApplication extends Application{
  private static boolean mIsRunning = false;

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

From Your activity do:-

public void onCreate(Bundle savedInstanceState){
  MyApplication.setRunning(true);
}

public void onDestroy(){
  MyApplication.setRunning(false);
}

And you can check like this:-

if(!MyApplication.isRunning()) {
    Intent i = new Intent(context, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}
VikasGoyal
  • 3,308
  • 1
  • 22
  • 42
0

I dont know if it is correct way or not but following code worked for me.

private boolean isApplicationAlive(Context context) {
    boolean applicationAlive = false;
    ActivityManager m = (ActivityManager) context.getSystemService( context.ACTIVITY_SERVICE );
    List<ActivityManager.RunningTaskInfo> runningTaskInfoList =  m.getRunningTasks(10);
    Iterator<ActivityManager.RunningTaskInfo> itr = runningTaskInfoList.iterator();
    while(itr.hasNext()){
        ActivityManager.RunningTaskInfo runningTaskInfo = (ActivityManager.RunningTaskInfo)itr.next();
        String activityName = runningTaskInfo.topActivity.getShortClassName();                
        if(activityName.equalsIgnoreCase(".activities.MainActivity")) {
            applicationAlive = true;
        }

    }          
    return applicationAlive;
}
Tushar Thakur
  • 956
  • 3
  • 15
  • 32