I've implemented a service that does an asynchronous sync between my application and google docs. I want to update the top level activity of my application when the sync is complete. However because of the service it's possible that the app could be be in a unknown state. Is there a way to make the top level activity, whatever that may be, recreate itself from an asynchtask in a service.
In your Service, just call startActivity(new Intent(this, TopLevelActivity.class))
. This will launch your TopLevelActivity if it's not already running, or call your TopLevelActivity's onNewIntent()
method if it is already running, and bring it to the top. You can override onNewIntent()
and capture the Intent if you want to be notified.
The correct way to do this, I believe, is to use a Remote Interface using AIDL: http://developer.android.com/intl/zh-CN/guide/developing/tools/aidl.html
You make a simple *.aidl file with the interface in it, and the Android tools will generate the Interface class for you. It will allow you to register Callback methods. Much cleaner than tossing startActivity intents back and forth.
You should broadcast an Intent from the service and then have the Activity be a broadcast receiver.
Here is a write up on BroadcastReceiver.
Hope this helps.