Rich
Rich

Reputation: 4207

Android Application, Activities, and UI Thread

I'm working on an Android project that consists of several different Activities all associated with the same Application. We have some common tasks coded in the Application so that we don't have to duplicate them, and one of these tasks is regularly verifying a TCP connection to a dedicated server -- a heartbeat, I suppose.

If it's detected that the connection to the server is lost, I need to notify the user and I'd like to do this in a way that doesn't require me to check all the possible activities to see which is currently "on top".

Is there a way to call runOnUiThread() on whatever activity may be on the UI thread without knowing it explicitly??

Thanks,
R.

Upvotes: 0

Views: 137

Answers (4)

marwinXXII
marwinXXII

Reputation: 1446

You can notify your Activities by sending Intent and registering BroadcastReceiver in each Activity you want to be notified.

service or application can be your context:

Intent i = new Intent("MY_ACTION_FROM_SERVICE_STRING");
context.sendBroadcast(i);

activity:

public class MyActivity extends Activity {
    private ActivityBroadcastReceiver recvr;

    public void onReceiveCommand() {
        //do something
    }
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        recvr = new ActivityBroadcastReceiver(this);
        this.registerReceiver(recvr,
            new IntentFilter("MY_ACTION_FROM_SERVICE_STRING"));
    }
}

receiver:

public class ActivityBroadcastReceiver extends BroadcastReceiver {
    private MyActivity target;
    public ActivityBroadcastReceiver(MyActivity target) {
        this.target = target;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        target.onReceiveCommand();
    }
}

Upvotes: 1

Samuel
Samuel

Reputation: 17171

Regularly verifying a TCP connection

Sounds like this should be implemented using a service..

If the alert is very simple, and has minimal importance, I would suggest using a Toast.

If the alert is crucial, but it doesn't require the user's immediate attention, use a Notification.

If the alert demands immediate user attention, you should use a Dialog. You won't be able to start a dialog directly from a service or broadcast receiver because they don't have a window associated with them, but you can use an intent to start an activity on a new task. You can style the activity to be whatever you want. It could even look like a dialog box (or show a dialog box when it's started). Starting the activity in a new task will make sure the user can navigate back to whatever they're doing.

Upvotes: 2

waqaslam
waqaslam

Reputation: 68187

One of the simple way (which I've adopted for my project too) is to create a base Activity class (preferably Abstracted) and then make your normal activity classes to extend it. By this, you may put a general piece of code in the abstracted class which may help you to detect currently visible activity.

Moreover, you may set a BroadcastReceiver in your base activity class which will always be ready to listen broadcasts regardless of setting it individually in your child activities and then set it to listen for broadcasts sent from your tcp thinggy.

Upvotes: 1

Marcin Orlowski
Marcin Orlowski

Reputation: 75635

Do not do any trickery. Implement Observer pattern, so any activity would register its listener in onResume() and unregister in onPause() and whatever will happend your Application object code needs just to tell about that to registered listeners, no matter what Activity they are in

Upvotes: 0

Related Questions