Alright, So I have an application that receives an intent in a Broadcast Receiver, but what I need to do with it, I cant do without extending Activity, so I launch an activity from that receiver, but how can I get it to do what I want it to do, without starting any UI of any kind? Thanks!
Use a Service
Related
I want to call the start Activity in BroadcastReceiver in android. I know it is a service and we cant call an activity from a service.
I want to call the start Activity in BroadcastReceiver in android.
Call startActivity() on the Context passed in as a parameter.
I know it is a service
A BroadcastReceiver is not a Service.
and we cant call an activity from a service.
Yes, you can call startActivity() in a Service.
However, calling startActivity() from either a BroadcastReceiver or a Service may not be what the user wants. You have no idea what the user is doing, and you might be taking over the foreground in the middle of something else. Please only use this in places where the user will value the interruption, or offer alternatives (e.g., a SharedPreference to indicate that you should use a Notification instead of starting an activity).
I'm developing a service and I've been following an example that unfortunately uses an Activity to get its work done. The example uses startActivityForResult() to get values after doing something. I'm wondering do I just use BroadcastReceiver to accomplish the same thing? sendBroadcast(intent) and then capture the broadcast to do whatever?
Yes, for me is the best solution, but you can bind the service to the activity if you want another solution.
Ok I am stuck. Can someone please point me in the right direction? I have no idea where to start with pendingintents. I need to start custom service that sends some data back to the activity that started it. How would I do that?
I would probably register a Broadcast Receiver in my Activity and if I needed to communicate from Service to Activity, send a broadcast from the Service and the Activity's receiver would pick it up as long as the Activity was currently running. This, by the way, would not require the use of a PendingIntent. PendingIntents are more used with alarms from the AlarmManager or with Notifications.
You should consider using AsyncTask if you expect your Activity to be active (visible) all the time the Service runs.
I'm trying to start a Service from my Activity to look out for changes on a web page, it's a private app so I don't bother the battery life...
But I'd like to pass data from my Service to my Activity... I can't seem to find a way to call the Activity from my Service. How can I achieve this?
As Alex indicated, you can bind to the service and pass some sort of listener or callback to the service to use on events.
Or, you can use a broadcast Intent, perhaps using methods like setPackage() on the Intent to limit the scope of the broadcast.
Or, you can use createPendingResult() to create a PendingIntent that you pass as an Intent extra to the service -- the service can then use that PendingIntent to trigger onActivityResult() in your activity.
Or, you can use a ResultReceiver.
Or, you can use a Messenger.
(admittedly, I have not tried those latter two approaches, but I think they will work here)
One more alternative: if your service updates content provider, activity can be notified via ContentObserver. This would be enough if your service downloads some data from server and you simply want to display fresh contents in the activity.
Some ugly ways:
1.) If the activity has not started yet, then use intent and startActivity, but remember intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2.) Otherwise if the activity has started already, you can write your own callback method in the activity and register the method in the service, then direct call the method in the service.
Hope to find some smart way.
I think broadcast also work well, you can write a static inner class for receive broadcast and start activity. But it is also ugly in my opinion.
The ResultReceiver mechanism has been explained in another post :-
Restful API service
However it would not work in all cases. Please refer to my comment on that post. The limited scope broadcast or PendingIntent mechanism seem more suitable.
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.
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.
The correct way to do this, I believe, is to use a Remote Interface using AIDL:
https://developer.android.com/guide/components/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.