Using BroadcastReceiver - android

I want to know whether an app can be a BroadcastReceiver and sender? Please expain with an example.

Application can't be a BroadcastReceiver. BroadcastReceiver is an application component. But answer to your question is yes: you can send broadcasts from one component and receive it in another.
For ex. in activity:
Intent intent = new Intent(...);
sendBroadcast(intent);
In receiver:
#Override
public void onReceive(Context context, Intent intent) {
// here is your intent
}

Yes, it can. An example can be found here.

If by app you mean activity, so yes you can but you will have to embed your BroadcastReceiver in your activity and register/unregister it yourself. That way, you just need to add your activity as Activity in the Manifest and you activity will be able to receive a broadcast and send broadcast as well.
I m not too sure how it behaves in term of life cycle though. You will need to look it up if it s what you want.

Related

Activity Listen for broadcast receiver

So I have broadcast receiver that is getting started on boot. I have an activity that using the information being collected by the broadcast receiver. I want the activity to be able to update its recycler view every time the broadcast receiver is called, the problem is the activity has no reference to the broadcast receiver. Is there a way that I can have my activity listen for the broadcasts and update itself?
The only thing I can think of is having the activity run a repeating task that will try to update itself with new information. This doesn't seem like a good solution to me.
the best approach is to register a BroadcastReceiver - see documentation on this. In your case you'd want to Programmatically register a broadcast receiver so that the onReceive(Context context, Intent intent) from inside the Activity class. In this way, you can then update the Recyclerview as you desire. Something like:
public void onCreate(Bundle savedInstanceState){
...
IntentFilter filter = new IntentFilter();
//you may want to set whatever filters here...
//define the broadcast receiver
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//here you can update the RecyclerView as you desire using intent's data
}
};
//register the broadcast receiver
registerReceiver(receiver, filter);
}
I strongly recommend that you go through this nice BroadcastReceiver tutorial.
Enjoy.
The broadcast receiver registered for BOOT_COMPLETED action has nothing to do with the activity, it's a separate component. So, yes, you don't have a reference to your activity and you should not run any periodical task.
What I would do is to write the collected data to the database or shared preferences and then read it when your activity is actually on the screen.
If you use an SQLite database you can use a ContentObserver to notify your activity about changes to the underlying data. This works great with loaders.
In case of shared preferences you can use a OnSharedPreferenceChangeListener registered in your activity.

How get Activity form BroadcastReceiver in onReceive

I define BroadcastReceiver in an application manifest and the receiver receives events in onReceive as expected. However I do not create the receiver class so I can't pass any information about my activity which the receiver is supposed to control. Context parameter gives me only application context and no any activity reference. So what is right way to make communication between application activities and broadcast receiver?
The answer is a little twisted but I found it here as well
Inform Activity from a BroadcastReceiver ONLY if it is in the foreground
It seems working. Please close the question then.
You can communicate broadcast to activity from this:-
Intent intent=new Intent(context,YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

Android BroadcastReceiver without intent filters

I saw in few android ad networks sdks that they are declaring BroadcastReceiver with no intent filters. Something like this:
<receiver android:name="com.example.SampleReceiver" />
My guess is that such receiver would capture all possible events. So I've tried doing it myself and created a SampleReceiver:
public class SampleReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
System.out.println("Event captured: " + intent.getAction());
}
}
I've launched the app, tried to fire some events by doing various action on my phone and noticed that onReceive() wasn't called even once.
So the question is - how does such BroadcastReceiver without intent filters work? Maybe it require the intent filters to be created via code? If so, how? If not, then why isn't it receiving any events? What's going on here?
If you do not have some intent filters, the only way to receive something is to call the receiver explicitly.
This would look like this:
context.sendBroadcast(new Intent(context, MyBroadcastReceiverClass.class));
Another guy already answered this question in the following post:
https://stackoverflow.com/questions/10051256/broadcast-receiver-not-receiving
I think that the following question/answer should give you some clues:
Create an IntentFilter in android that matches ALL intents

send broadcast from Service to Activity?

I'm having a problem with sending a broadcast from a Service to an activity.
This is what I have in my Service class:
Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION);
sendBroadcast(intent);
I have many Activities and in one of my activities I have this:
class MyBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context ctxt, Intent i) {
System.out.println("received");
}
};
The problem I have is that my broadcast receiver doesn't receive anything!!
Help!
EDIT:
If I have many activities how can send a broadcast message to all of them. In other words can I apply the same broadcast receiver to all the activities !?
Like others said, you need to register the activity first to receive those broadcasts (see Flo's answer)
For your other quesition (re: EDIT). If you are taking the same action, you should create an overall Activity, and have your other activities extend that activity..
Then in this super class, implement the broadcast receiver registers on onResume and un register onStop..
You have to register the broadcast receiver before it can receive anything.
Have a look at this question.

How to receive the TIME_TICK intent

This isn't a very advanced question, I just don't understand how exactly to receive intents in Android. I want to update a clock app (only when it's in the foreground) every minute. I guess I have to create a class that extends BroadcastReceiver, and implement the method onReceive(Context context, Intent intent), and add an intent filter in my AdroidManifest.xml? How do I add that filter? Is that the only thing that makes the intent received ACTION_TIME_TICK?
As Android SDK reference states, you have to explicitly register for this intent with:
Context.registerReceiver().

Categories

Resources