• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Start activity in broadcast receiver

 
Ranch Hand
Posts: 643
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
chetan deol
Ranch Hand
Posts: 643
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

See below one way:

For example:



BR,
 
author
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Helio Padrao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Hey! You're stepping on my hand! Help me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic