3

I have an andoid-app that connects to a WebSocket-server when the user logs in. If the user is idle for some time or for whatever reason his session expires/terminates, the application gets notified through the WebSocket-connection and I want to take him back to the login-activity (there is no functionality in the application if he is not logged in).

I have looked into broad-casting Intents and the runOnUIThread() but can't seem to get it to work. An idea I have is to simply register the running activity (have all of them implement an interface for example) and make a call but that would mean all Activities would have to implement the interface. Most of them already inherits from a BaseActivity-class but not all of them.

I realize its bad practice to disrupt the user-flow abruptly, but none of the functionality will work unless the user have a valid session.

Im using the android-websocket (https://github.com/koush/android-websockets) library as a client if that makes any difference.

Thanks in advance Tomas

EDIT: Sometimes I would also like to inform the user in the form of a Toast or Dialog that a certain event happened on the server.

taracus
  • 393
  • 1
  • 5
  • 19
  • possible duplicate of [android start activity from service](http://stackoverflow.com/questions/3606596/android-start-activity-from-service) – silwar Feb 13 '14 at 12:23

3 Answers3

9

I did tests

  • you can startActivity from non-ui Thread
  • you can finish Activity from non-ui Thread

Look at the ACRA project - nice exception handling. In ReportExecutor in line 217 (https://github.com/ACRA/acra/blob/master/acra/src/main/java/org/acra/builder/ReportExecutor.java#L217) activity is started in completely new thread (without runOnUiThread).

I was too surprised why it is working :)

  • Permanent link for `ReportExecutor.java` https://github.com/F43nd1r/acra/blob/47b08c696fd84bbdd1ede6a6fe762cbfa42bfca0/acra/src/main/java/org/acra/builder/ReportExecutor.java#L218-L226 – Amos Wong Dec 25 '18 at 22:59
2

To start activity from background try following code

  Intent newIntent= new Intent(getBaseContext(), MainActivity.class);
  newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  getApplication().startActivity(newIntent);
silwar
  • 6,470
  • 3
  • 46
  • 66
2

I think any of these two approaches should work:

  • Use a Handler: Set a Handler in your Activity, override its handleMessage so when you send a message to it, you simply bring your Activity to the front (or start it). To bring it to front:

    Intent i = new Intent(this, MyMainActivity.class);
    i.setAction(Intent.ACTION_MAIN);
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);
    
  • Use a BroadcastReceiver: There's no reason why this shouldn't work, simply declare your receiver, register it for some action (might be customized) and start the activity the same way than above. Don't forget to unregister it once you're done or you close your app.

If you need an example on any, just ask for it.

nKn
  • 13,691
  • 9
  • 45
  • 62
  • The first approach would mean I have to set a Handler in every single Activity in my application now? Thats what I am trying to avoid. But I will try the second approach with the BroadcastReceiver and report back if I get it to work. Thanks ! – taracus Feb 13 '14 at 13:48
  • Indeed, it would mean it has to be `Activity`-dependent. In that case try the second one, if you need help just ask say it. – nKn Feb 13 '14 at 14:11