This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Android and the fly likes Start activity in broadcast receiver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Start activity in broadcast receiver" Watch "Start activity in broadcast receiver" New topic
Author

Start activity in broadcast receiver

chetan dhumane
Ranch Hand

Joined: Jan 07, 2009
Posts: 629

In BroadCastReceiver I wrote this function.
Can we have startActivityforresult in receive method.

context.startActivity(mIntent);

Will above function wait for the activity to finish on just continue to the next line of the code.


http://www.androcid.com/
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Chetan go through this. When you start an activity using startActivityForResult (which is not the case in your code as you are using startActivity), the call is asynchronous. But the lifecycle of a broadcast receiver ends when the onReceive method returns so your broadcast receiver might not be available when the started activity returns...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
chetan dhumane
Ranch Hand

Joined: Jan 07, 2009
Posts: 629

Ankit Garg wrote:Chetan go through this. When you start an activity using startActivityForResult (which is not the case in your code as you are using startActivity), the call is asynchronous. But the lifecycle of a broadcast receiver ends when the onReceive method returns so your broadcast receiver might not be available when the started activity returns...


Yeah I got you , then how to achieve the same.
Because I want it to be done like that only.
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

chetan dhumane wrote:
Ankit Garg wrote:Chetan go through this. When you start an activity using startActivityForResult (which is not the case in your code as you are using startActivity), the call is asynchronous. But the lifecycle of a broadcast receiver ends when the onReceive method returns so your broadcast receiver might not be available when the started activity returns...


Yeah I got you , then how to achieve the same.
Because I want it to be done like that only.

Two things:
1. A Broadcast Receiver is expected to return immediately. Making it wait is just wrong.
2. You wont be able to invoke startActivityforResult() on the context object passed to the onReceive() method of the Broadcast receiver.

[List of FAQs] | [Android FAQ] | [Samuh Varta]
Helio Padrao
Greenhorn

Joined: May 17, 2011
Posts: 5
Hey,

See below one way:

For example:



BR,
Satya Komatineni
author
Ranch Hand

Joined: May 11, 2011
Posts: 38
In general it is discouraged to start an activity from a background service or a broadcast receiver. This is because it will be jarring to the user that is looking at a screen and all of a sudden an activity pops up. The HIGHLY recommended method is to use the notification manager.

However this is possible as Helio pointed out in the earlier answer.

The relevent flags you want to read up on are:

flag_from_background
flag_activity_new_task
flag_activity_singletop

There is an additional consideration on a broadcast receiver. Here is what the documentation says on a broadcast receiver

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.


However it is entirely legitimate to do a "startService" in the broadcast receiver. This means the broadcast receiver is hanging long enough to start things. By extension you can think that the actvity will be started as well.

Here is some research on Broadcast Receivers and Long Running Sevices


My Web Site: satyakomatineni.com
Our Book for Tablets/Phones: Pro Android 3
Helio Padrao
Greenhorn

Joined: May 17, 2011
Posts: 5
I completely agree with you. It's not the best way to start an activity ...

In my case, I'm using RE to control which SMS is relevant to turn on/off or whatever I wanna.

Good idea to use notification manager.
Satya Komatineni
author
Ranch Hand

Joined: May 11, 2011
Posts: 38
Helio,

I think the world belongs to the brave. So one should try.

When i was doing the research for long running services, i have found that the broadcast receiver could be invoked from an alarm while it the device is asleep just for servicing the alarm. And it was advised to me that I should obtain a wake lock in the receiver before starting a service. Otherwise I was told the device could go back to sleep.

So this might happen both for an activity start and I am not entirely clear what happens to subsequent notifications as well under such circumstances.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Start activity in broadcast receiver