1

I want to detect whether app is currently running (i.e, either on background or foreground) or not on click of notification. I have implemented Broadcast receiver where i want to check app state(running or not). I have already tried this check android application is in foreground or not? . But it is not working as excepted. Is there any other way to detect this?

Adil Saiyad
  • 1,582
  • 2
  • 17
  • 34
Nainal
  • 1,728
  • 14
  • 27
  • There are many ways to implement on the link you shared, which one is not working? I think the one who uses Application class gonna works. – Kharda Aug 09 '17 at 11:16
  • Possible duplicate of [Checking if an Android application is running in the background](https://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background) – Kharda Aug 09 '17 at 11:34
  • 1
    @Kharda This will check whether app is in foreground or background and not whether app is running or not – Nainal Aug 09 '17 at 11:38
  • how could the app be not running if a broadcastreceiver is started? – Dokumans Aug 09 '17 at 12:16

3 Answers3

3

You should take a look at a new component Handling Lifecycles. You can easily check the current state of activity, fragment, service and even process ProcessLifecycleOwner.

For example:

class CustomApplication extends Application {

    private static Lifecycle lifecycle;

    @Override
    public void onCreate() {
        super.onCreate();
        lifecycle = ProcessLifecycleOwner.get().getLifecycle();
    }

    public static Lifecycle.State getCurrentState (){
        return lifecycle.getCurrentState();
    }
}

And then you can check a state:

    if(customApplicationgetCurrentState().isAtLeast(Lifecycle.State.RESUMED)){
               //do something 
    }

You can also add an observer to listen changes.

public class CustomApplication extends Application {

private static Lifecycle lifecycle;

public static Lifecycle.State getCurrentState() {
    return lifecycle.getCurrentState();
}

@Override
public void onCreate() {
    super.onCreate();
    lifecycle = ProcessLifecycleOwner.get().getLifecycle();
    lifecycle.addObserver(new SomeObserver());
}

public static class SomeObserver implements LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    void onCreate() {
        Log.d("Observer", ": onCreate");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    void onStop() {
        Log.d("Observer", ": onStop");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    void onResume() {
        Log.d("Observer", ": onResume");
    }
}
}

Adding Components to your Project

Onregs
  • 420
  • 3
  • 16
0

This doesn't work this way - your application should start (via the same flow as usual) if it's no running.

You can pass some extras on the intent from your BroadcastReceiver into the app and it should be handled the way you need.

This is the only way to do it.

MarkySmarky
  • 1,609
  • 14
  • 17
  • what type of extra, can you please elaborate? I want to check whether app is running or not. – Nainal Aug 09 '17 at 11:28
  • give me an example of what you want to do if the app is running and if the app is not running... – MarkySmarky Aug 09 '17 at 11:29
  • I want to open different activities when app is running and when app is closed – Nainal Aug 09 '17 at 11:33
  • If app is in foreground and background I want to open suppose a ActivityD and if app is closed I want to open the app from launcher activity. – Nainal Aug 09 '17 at 11:51
  • I understand that, but what functionality do you want to present if the app is running/not running. What I am trying to say is that in 9 yrs of developing Android apps I never had to do it - something is not right in your flow – MarkySmarky Aug 09 '17 at 14:14
0

I can suggest you what I have done when the notification comes. override super method onNewIntent(Intent intent) in your activity that you have a pass in your pending intent, means desired activity that will be open when the user press on a notification. And here when your application is in the foreground then this method will be called, and if your application is killed then desired activity onCreate() will be called. And one more thing is to make your activity single top in Manifest.

In Manifest:        <activity
            android:name=".activities.TokenActivity"
            android:label="@string/app_name_short"
            android:launchMode="singleTop"/>

In Activity:    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        loadCouponFragment(intent.getExtras());
    }
Anand Saggam
  • 57
  • 1
  • 8