Is it possible to startActivityForResult from something other than activity?
Eg: we have broadcastReceiver class which starts activity, those activity takes some action, finishes returning result which this broadcastReceiver class should interpret according to result itself(finish app or perform more sophisticated actions).
BroadcastReceiver cannot handler onActivityResult callback.
A better idea would be to create a different BroadcastReceiver that handles the response of the activity started by first Receiver.
So you will have one Receiver that starts an activity on some action, the activity, in turn, sends a broadcast to other receiver which is supposed to perform further tasks
Related
I have a BroadcastReceiver, and the onReceive is called from two different postExecute methods in two different asyncTasks, in two different Activities.
I have a third activity that is running all the time called HomeActivity, and I want to publish some text to the HomeActivity's UI from the onReceive method.
Is it possible? I know that the context parameter is the context of the activity who raised the onReceive, but I want to access the HomeActivity's UI.
Here is the code
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// here I want to publish some text to the HomeActivity
}
}
any ideas? thanks in advance
You want to change text in your running activity based on what you receive in the onReceive of the BroadcastReceiver? Right? One way is that you can use LocalBroadcast. See LocalBroadcast Manager.
For how to implement is, there is a great example on how to use LocalBroadcastManager?.
LocalBroadcast Manager is a helper to register for and send broadcasts of Intents to local objects within your process. The data you are broadcasting won't leave your app, so don't need to worry about leaking private data.`
Your HomeActivity can registers for this local broadcast. From the MyBroadcastReceiver you send a LocalBroadcast from within the onReceive (saying that hey, I received a message. Do you want to do something now activity). Then inside your Activity you can listen to the broadcast. This way if the activity is in the forefront/is active, it will receive the broadcast otherwise it won't. So, whenever you receive that local broadcast, you may change the text etc, if activity is open.
If I register for an Intent in the AndroidManifest.xml (specifically DOWNLOAD_COMPLETE from the DownloadManager). If this BroadcastReceiver is called while the application was previously killed, what state will the main Activity be in? Will it even be created?
It will call whatever you define in the onReceive portion of the broadcast receiver. If you want it to call MainActivity.class then you can easily do that.
I have managed to get an Activity to start from my onReceive() methdod, but I really need to do a startActivityForResult();.
Is there any way I could do this?
On a side note, how would I make my app become a 'camera' app, as in it would appear when an app started the intent to take a picture?
The important thing to know about broadcast receivers is that you should not add long running processes in it, because after something like 5 seconds your app will crash.
The best thing to do in your case is to intent to other Activity from your broadcast receiver, and from that activity use startActivityForResult(), get the picture and continue from there...
startActivityForResult can only be called from an Activity since it is defined in the Activity class and require instance of activity.
You can only call startAcivity() from broadcast receiver since in onRecieve() you only have access to generic context object and it does not have startActivityForResult method defined in the class..
There's a service which runs always and listens to some events, will push them into a log someday. I would like to display those events in the main actvity when it's running, but how to send the event details to the activity? You don't receive the intent itself, when you send it to the activity with Context.startActivity(), so that you can't retrive the data with Intent.getXxxExtra().
(The activity would "subscribe" and "unsibscribe" to the events in onStart() and onStop() with an intent sent to the service, so that the events wouldn't open the activity if it's not in foreground)
Or is there any other way to send data (20-30 characters long String) from a service to an intent?
Issue a Broadcast from your Service and implement a private BroadcastReceiver in your activity.
Yes, you can use the Handler class for message passing between your Service and your main Activity.
I am currently firing an Intent to a Broadcast Receiver which in turns starts an Activity.
Then from the same Service another Intent is fired to a Broadcast Receiver thats in the Activity.
The problem is that the Activity isn't getting the Intent meant for it because it is fired before it is alive and the Broadcast Reciever is registered.
I was wondering is there anyway to make sure an Activity is alive before sending an Intent?
Or any other solution to this?
Why not start the activity if it is not yet alive?
The general mechanism to start a new
activity if its not running— or to
bring the activity stack to the front
if is already running in the
background— is the to use the
NEW_TASK_LAUNCH flag in the
startActivity() call.
That or simply give the activity a chance to start before firing the Intent.
Alternatively you could try using sendOrderedBroadcast to retrieve data back from the broadcast and then possibly do a retry.
public abstract void sendOrderedBroadcast (Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)