I created a broadcast receiver. After that in onReceive, I received an information and called a method in the class.
my question is when I call that method I want to call only once but why it calls more than one time?
Are there any way we can determine that onReceive receive only one ???
EDIT
my method that I call in onReceiver is update map location in class extend MapActivity
I also register my broadcast service in manisfest.xml
I want to add comment to the answer but i can't i don't know why
how can we determine to our broadcast receiver to receiver only one??
thanks
you can maintain a bool variable and when you get call first time in the method, make this bool true and next time before calling the method you can put check there using this bool variable.
If you are not registering your receiver in the manifest file then you can use this bool value before registering the receiver.
Related
I want to start service if screen is on.I think I have to use Intent.ACTION_SCREEN_ON. But I'm not sure where should I declare it in AndroidManifest or through RegisterReceiver method? As I understood if I will declare this action in the AndroidManifest my service will be started even if user hasn't reached specific point in my app cause action was committed. So if want to start service after user has reached that specific point and also screen is on I should use RegisterReceiver method, right?
But I'm not sure where should I declare it in AndroidManifest or through RegisterReceiver method?
ACTION_SCREEN_ON only works via registerReceiver().
Is it possible to call the BOOT_COMPLETED broadcastreceiver programmatically? I use it in the normal way but I want to execute it another time during runtime.
You can't send ACTION_BOOT_COMPLETED yourself. According to the docs:
"This is a protected intent that can only be sent by the system."
http://developer.android.com/reference/android/content/Intent.html#ACTION_BOOT_COMPLETED
You can of course send your own intent, and trigger the same code to be called.
You are welcome to call sendBroadcast() to trigger your own BroadcastReceiver.
Usually, it is simpler just to have the common code -- needed both at boot time and at other times -- in some static method or helper class. Then, you do not need to actually call sendBroadcast(), as you can just use the static method or helper class to do the work.
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.
I am stuck up just now; issue is my one of activity has register one private broadcastReceiver and I need to unregister it and as usual I cannot change that file.
My approach is get all broadcast receiver information () for that intent by Package Manager.queryBraodcastReceiver method.
Now I want to get broadcast receiver object from it.
Is there any other approach to work out this problem?
Is there any other approach to work out this problem?
Fix the activity to unregister its receiver. You cannot do this from anywhere else in your app.
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..