0

I'm using a running service to detect whether network is available or not. When it is not available, it calls an activity to start that displays a blank screen with "no network available" on it. When the network is back, it sends a broadcast to finish this activity.

The only problem is that this activity may start at any time (as a popup), even when using other apps. I want it to start (or be visible) only if the network is out and my app is in the foreground. Any help?

3 Answers3

2

One option would be to have your foreground activity register for the broadcast, and then display the relevant notification from within the activity.

Alternatively you could start your service when your foreground activity starts/resumes (i.e, onResume), and stop it when your activity leaves the foreground.

You can use START_STICKY in your service to ensure it stays around until you stop it, like so:

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    //On start work here
    return START_STICKY;
}

and then stop the service using stopService when your activity leaves the foreground (i.e onPause).

If you need the former behaviour across multiple activities you can register broadcast receivers programmatically:

BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if(MY_ACTION.equals(intent.getAction()))
                {
                    //show appropriate dialog
                }
            }
        };

IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(MY_ACTION);
registerReceiver(myBroadcastReceiver,myIntentFilter);

You can unregister like so:

unregisterReceiver(myBroadcastReceiver);

You could extend Activity and make your own custom subclass that reuses similar code to register and unregister whilst entering/leaving the foreground. Or you can extract this into utility methods/classes and call from the appropriate places.

Pring
  • 61
  • 4
  • Displaying the relevant notification from the activity worked, thank you. However, I have 9 activities and will have to add a received in each one of them, if I need to respond properly to network outage at all stages. – Alex Kayal Dec 26 '13 at 09:45
  • You can register and deregister broadcast receivers programmatically. See above. – Pring Dec 26 '13 at 15:28
1

I think you need Shared Preference to do this. store one Boolean value on finish you activity (you can use onpause() or onStop()) and for showing popup check the value and do what you want

for understnding to use sharePreference see this and developer.android.com

Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • The problem is, when an activity starts, onCreate() is called. When it is called, the content view of that activity is gonna show regardless of what you are doing with the device. If there is a way to stop the content from showing until the app is explicitly in the foreground, it would solve it. – Alex Kayal Dec 25 '13 at 17:47
  • call `this.finish` in `onCreate()` method to close app. check the value and if you want to close before activity goes up call `this.finish()` – Shayan Pourvatan Dec 25 '13 at 17:49
  • You can check the SharedPreference in the Service before starting the "no network available" Activity, this solution can work. – Steve M Dec 25 '13 at 18:33
1

Try the following:

Intent intent = new Intent(this, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);

This worked in the context of my own app already running, I'm not sure if it will start your app if it is not already running in the background

EDIT: Not sure if I understand your question entirely. If you only want this activity to come to the foreground while your app is in the foreground, get rid of the addFlags line, also you can do some boolean stuff to check if your app is in the foreground like so, this way your code won't even run if the app isn't in the foreground.

EDIT: There are a few ways to check if your app is in the foreground, the link I posted above has one such solution, another one is to create a static boolean isForeground variable: in the onResume() methods of your app set isForeground = true and in onPuase() set isForeground = false. This isn't the best practice, using ActivityManager is better, but for purposes of testing this should be ok.

Then have something like the following:

if(isForeground){
    //Start your activity
}

This should be quick to write, if this is the behavior you want, I would recommend replacing the isForeground static variable with the test for foreground provided by ActivityManager in the link I posted.

Community
  • 1
  • 1
mike
  • 1,318
  • 3
  • 21
  • 41
  • This is precisely what I'm using, with this flag (found this on this website somewhere). Would you think that if I do not add that flag, then it may not start until my app is in the foreground again? – Alex Kayal Dec 25 '13 at 17:54
  • Sort of, if you remove the flag, the activity will still get started, but it will happen in the background. So when you go to the app, that activity will be in front. If you don't want the activity to launch at all if the app is not in the foreground, then you need to run some boolean logic to test if your app is in the foreground. There are a few ways to do this, I suggested one link in my edit – mike Dec 25 '13 at 17:57
  • OK well, I have removed the flag, which prevented the activity from starting like a popup, but it also did not start when I brought the app to the foreground. – Alex Kayal Dec 25 '13 at 18:08
  • I'm a little confused by what you're trying to do. Do you want the activity to start in the background if the app is in the background, or do you want the activity to only start if the app is in the foreground? – mike Dec 25 '13 at 18:13
  • I want the app to to display the contents of this activity when the network is out-- When the app is in the background, there is no need to display this content. As in, there is no point of displaying an activity showing that network is out while the user might be user other apps. – Alex Kayal Dec 25 '13 at 18:21
  • Edited my answer, added some code, I'm still not sure if this is the behavior you want, but I provided a quick solution that is easy to write, if that is the behavior you want, you should spend a little more time revising the code though – mike Dec 25 '13 at 18:25