Android Refresh Activity

I had an issue where I was having a problem getting an activity to refresh or recreate itself, when being called from a BroadcastReceiver.   The odd thing is that if I launched using Eclipse, it would work and refresh.  However launched normally it just wouldn’t refresh.   The basic use case is that I needed the main screen to refresh once some servers were discovered.  At this point it needed to do some auto configuration based on the servers found and refresh the screen.

I ended up using the Handler.postDelay method, to wait until after the time out for the server discovery, and then refresh the screen.  Since Handlers run in the UI thread this seems to work.  Note I tried calling a context.runOnUIThread from the broadcast receiver originally and it didn’t work when not launched from eclipse.

private class AutoConfigureHandlerRunnable implements Runnable {
   public void run() {
      if (SerenityApplication.getPlexMediaServers().isEmpty()) {
         Toast.makeText(MainActivity.this, "No servers discovered. Use settings to configure manually.", Toast.LENGTH_LONG).show();
      Server server = SerenityApplication.getPlexMediaServers().values().iterator().next();
      String ipAddress = preferences.getString("server", "");
      if ("".equals(ipAddress)) {
        Editor edit = preferences.edit();
        edit.putString("server", server.getIPAddress());
        Toast.makeText(mainContext, "Auto configuring server using " + server.getServerName(), Toast.LENGTH_LONG).show();

The code is kicked off from within the onStart() method of an Activity.

    autoConfigHandler.postDelay(new AutoConfigureHandlerRunnable(), 2500);

So basically, if there isn’t already a server configured, it will check the discovered servers after 2.5 seconds, and configure the default to the first server it finds. For most people this should work fine as most will only have 1 Plex Media Server on their local network. For those that have more than one, they can go into the Settings and change the configured server to one of the others that were discovered. A message is displayed to the user telling them which server was choosen.

I’m still not sure why things worked from the Broadcast Receiver when running through eclipse, but didn’t when running normally, but the postDelay method is a nice work around to the issue. Is there another way I could have handled this? If so leave comments below.

For users of Serenity, this feature will be coming in a 1.3.6.

This entry was posted in android, googletv, plex media server, serenity. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s