Start Activity for result in a Broadcast Receiver? - android

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..

Related

call method while application closed

How can I call a method while my application is closed on Android?
I've tried:
Onpause(), OnDestroy(), and OnStop(). With no luck.
What I want is receiving something from the database and do my reaction
based upon when the application is closed.
I think you can use broadcast or service to do it.
You could create a base activity (BaseActivity class) from which all your activities would have to be derived. Then inside BaseActivity.onCreate - you would increase some SharedPreferences counter, and inside BaseActivity.onDestroy you would decrease it. Now when that counter is equal to zero, you might assume your application is closed - but the process might still run in the background.
What I want is receiving something from the database
now I assume you already know how to "receive something from database". This could be background Service, where you could check SharedPreferences and do your processing.
What is the trigger?
Part 1 : Intent -
By example, you can set an alarm, that launch an Intent.
Part 2 : Broadcast Receiver - You must create a Broadcast Receiver (which reacts to an intent).
Part 3 : Service - The Broadcast Intent will launch a service that will execute when the application is not open.

android intent startActivity() and sendBroadcast()

What are the differences between startActivity() and sendBroadcast()?
We can use startActivity() to start a activity.sendBroadcast() can do this too.what are the differences?
They do look similar, cause they have same function: send a message to receiver.
You can tell the difference from the receiver.
startActivity() can only has one receiver, in other words, only one receiver can receiver it's message(intent).
But for sendBroadcast(), multiple receivers can receive the message.
Here we go,
startActivity() - gets an intent and open its attached activity.
You have two ways how to use it - explicit - by adding activity name to the intent or implicit by adding only action(Ex. Intent.ACTION_SENDTO).
activity.sendBroadcast() - broadcast is a totally different thing than intent.
With broadcasts you can communicate with any registered broadcast receiver, in practice - sending messages to different component within/outside your application.
sendBroadCast() sends a global broadcast that is to be picked up by any BroadcastReceivers that are set to receive that broadcast.
startActivity() attempts to start an Activity based on either the class name you specify or the Intent Action (which is a String).
REF : sendBroadscast VS startActivity. What is the difference?

Run startActivityForResult from BroadcastReceiver?

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

Many PendingIntents, BroadcastReceiver and activity

I'm writing a simple reminder app. All reminders are stored in DB. I have a service that query DB and make a pendingIntents in AlarmManager with extras and different timestamps. Also I have a Broadcast Receiver to catch the Intents from AlarmManager. This Broadcast Receiver start a reminder Activity with options for reminder (dismiss, snooze, etc).
Now this scheme work, but not as good as I think it should. If I have a reminder activity in foreground, then new reminder activity starts upon it (current goes to background). I want to not override the current activity with new one and just notify the user, that there are some new reminders that will show after the current.
As I think, I've found a good solution for my task:
1) I've set in AndroidManifest that my reminder activity launchMode is "singleTop". More about launchMode is here http://developer.android.com/guide/topics/manifest/activity-element.html
In two word, if my Broadcast Receiver tries to start activity that already on foreground, it calls onNewIntent, not onCreate.
2) In my activity I've to override the onNewIntent method and store all incoming intents (from broadcast) in ArrayList .
3) Before finish() I've to remove current Intent from ArrayList and when it's size become zero I've actually finish() the activity.
One important addition. In broadcast receiver intent must have FLAG_ACTIVITY_SINGLE_TOP, like:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Without it if no main activity present onNewIntent will not be called. As I see, this is knows issue: http://code.google.com/p/android/issues/detail?id=4155
Bug found in Android 1.6 and still present. So now it is feature :)
Sorry for my English, it's easy for me to read, but hard to write :)

How BroadcastReceiver is different from Intent

Hello I see contradicting definitions. Android experts, can you explain this to me please?
1) If BroadCastReceiver is a component in android that responds to intents, then I can as well register an filter for activity in androidManifest xml file and have it do my job based on intent like battery low, no network, orientation change etc. these are intents I might be interested to react in my code.
2) Why register whole another filter for BroadcastReceiver in androoidManifest.xml and perform action at onReceive() inside the BCRCVR class?
3.Can we really perform intent driven operations in an activity? yes right?
I guess the title should be "How BroadcastReceiver is different from Activity".
In my opinion, Broadcast receiver is independent unit, because sometimes you don't want the system to create a new Activity object just to handle arrived intent. Moreover, you don't your activity to be shown. Broadcast receivers are independent and can be used outside any Activity. Activity is more about user interface and broadcast is about handling events.

Categories

Resources