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
Related
When I am working with broadcast receivers, I get a little confused about methods like sendBroadcast and registerReciever. Both gives the same result, and working functionality are also the same. But, what is the reason behind to work with both?
For example, If I try to get the result of my battery level, I am using the coding like
public void onButtonClick(View view)
{
IntentFilter intentFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
BraodcastReceiver br=new BroadcastReceiver();
registerReceiver(br,intentFilter);
}
or
public void onButtonClick(View view)
{
Intent intent=new Intent(Intent.ACTION_BATTERY_CHANGED);
sendBroadcast(intent);
}
What are the differences between this two methods? How they will work? Can you please give me the reasons?
My BroadcastReceiver class:
public class MyBroadcastClass extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
Log.d("BatteryLevel",level);
}
}
First of all you have to understand what is broadcast receiver in android.
This one example
I will explain shortly.
The first example is registering as the name indicates. So after registering a particular broadcast , it will listen for any broadcast with that ACTION you provided with intentFilter. The working is same as the callback mechanism.
The second example is sending broadcast . Sending broadcast means you broadcast something, say battery change(OS Level) ,It will broadcast with an ACTION.
SO send broadcast will send some data with Action , if we listen a broadcast with Action then it will trigger on BroadcastRecicver class
I suggest you to go through the developer docs https://developer.android.com/reference/android/content/BroadcastReceiver.html
you will get the clear idea.
coming to your question
registerReceiver() is used to register your broadcast receiver to a particular action eg- Intent.ACTION_BATTERY_CHANGED or you can define your own.
What it mean is that whenever any APP will send a broadcast (send using a sendBroadcast() method) to this action your broadcast receivers onReceive() method will get called.
Alright, so i am having some problems trying to get a broadcast receiver and service to work properly with screen off and screen on.
What i am trying to do is start something when the screen goes off or when the screen goes on. I got it to work from an activity for testing, but the activity must be currently running. I need it to start from the background pretty much.
Now, i know that using the intent filters in the manifest does not work for screen_off and on. How would i be able to do this? I guess this would work sort of like a lockscreen...
Screen off --> starts something (example activity or create a log message as a toast wouldn't work)
Add a receiver:
public class BroadcastReceiverScreenListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Objects.equals(intent.getAction(), Intent.ACTION_SCREEN_OFF)) {
** Do your stuff**
}
}
From the docs:
You cannot receive this through components declared in
manifests, only by explicitly registering for it with
registerReceiver(BroadcastReceiver, IntentFilter)
This is a protected intent that can only be sent
by the system.
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().
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.
I'm trying to get a BroadcastReceiver invoked when the screen is turned on. In my AndroidManifest.xml I have specified :
<receiver android:name="IntentReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"></action>
</intent-filter>
</receiver>
However it seems the receiver is never invoked (breakpoints don't fire, log statements ignored). I've swapped out SCREEN_ON for BOOT_COMPLETED for a test, and this does get invoked.
This is in a 1.6 (SDK level 4) project.
A Google Code Search revealed this, I downloaded the project and synced it, converted it to work with latest tools, but it too is not able to intercept that event.
http://www.google.com/codesearch/p?hl=en#_8L9bayv7qE/trunk/phxandroid-intent-query/AndroidManifest.xml&q=android.intent.action.SCREEN_ON
Is this perhaps no longer supported?
Previously I have been able to intercept this event successfully with a call to Context.registerReceiver() like so
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// ...
}
}, new IntentFilter(Intent.ACTION_SCREEN_ON));
However this was performed by a long-living Service. Following sage advice from CommonsWare I have elected to try to remove the long-living Service and use different techniques. But I still need to detect the screen off and on events.
Following sage advice from CommonsWare
I have elected to try to remove the
long-living Service and use different
techniques.
Actually, I believe my advice was more of a light blue... :-)
But I still need to detect the screen
off and on events.
There are certain events that Android does not want to start up new processes for, so the device does not get too slow from all sorts of stuff all having to run at once. ACTION_SCREEN_ON is one of those. See this previous question for light blue advice on that topic.
So, you need to ask yourself, "Self, do I really need to get control on those events?". The core Android team would like it if your answer was "no".
This is the best example I've found http://androidexample.com/Screen_Wake_Sleep_Event_Listner_Service_-_Android_Example/index.php?view=article_discription&aid=91&aaid=115
Actullay i was faceing this issue but i resolve it succeessfully
1) start service from your main activity
Intent i = new Intent(MainActivity.this, UpdateService.class);
startService(i);
2) register reciver in service class.
#Override
public void onCreate() {
super.onCreate();
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReciever();
registerReceiver(mReceiver, filter);
}
3) Done