Android : Broadcast not being received - android

I am attempting to send a broadcast from an IntentService, I am having no luck.
MainActivity
private void registerBroadcastReceiver() {
Context context = this;
IntentFilter filter = new IntentFilter();
filter.addAction("io.a.spike");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("******* received broadcast *******");
}
};
registerReceiver(receiver, filter);
}
IntentService
#Override
protected void onHandleIntent(Intent intent) {
System.out.println("send broadcast");
Intent notificationIntent = new Intent();
notificationIntent.setClass(this, MainActivity.class);
notificationIntent.setAction("io.a.spike");
sendBroadcast(notificationIntent);
}
I am not getting to the onReceive method within my registerBroadcastReceiver method. Any guidance would be most appreciated.
Thank you.

For getting your onReceive() called, you will have to add these -
add your BroadcastReceiver to into manifest, in this way -
<receiver android:name="YourReceiver"> <intent-filter> <action android:name="YourAction" /> </intent-filter> </receiver>
call registerReceiver() in your activity.
Also, don't forget to call unegisterReceiver() to avoid leaks.
Also, refer Android BroadcastReceiver without intent filters

You can use LocalBroadcastManager
Create instance
LocalBroadcastManager localBroadcastManager =
LocalBroadcastManager.getInstance(this);
Register Receiver
localBroadcastManager.registerReceiver(new YourReceiver(),new
IntentFilter("YourAction"));
Send Broadcast
localBroadcastManager.sendBroadcast(intent);
UnRegister Receiver
localBroadcastManager.unregisterReceiver(mybroadcast);

Related

How can I start receiver class from an Activity in android?

I have an activtiy and a receiver.class with broadcastreceiver. I would like to start receiver from mainactivity but I could not do this with my solution.
Here is may way
Intent iinent= new Intent(MainActivity.this,MyReceiver.class);
startActivity(iinent);
When I try this app says me Unfortunately app has stopped'' and my logcat says this intent' line s incorrect.
Is there another way to do this?
For Sending Broadcast Receiver Broadcast Receiver must be registered
in Manifest
<receiver android:name="TestReceiver">
<intent-filter>
<action android:name="ACTION_NAME"/>
</intent-filter>
</receiver>
In Activity
IntentFilter filter = new IntentFilter();
filter.addAction("ACTION_NAME");
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//do something based on the intent's action
}
}
registerReceiver(receiver, filter);
SendBroadcast
Intent intnet = new Intent("ACTION_NAME");
sendBroadcast(intnet);

Android BroadcastReceiver unable to receive Screen_On and Screen_Off broadcasts

I am trying to get my BroadcastReceiver, created inside of a service, to receive Screen_On and Screen_Off events. It is not working, here is what I am trying right now.
//OnCreate Method for the service
#Override
public void onCreate() {
super.onCreate();
myBroadcast = new MyBroadcastReceiver();
IntentFilter screenOn_Off = new IntentFilter();
screenOn_Off.addAction(Intent.ACTION_SCREEN_OFF);
screenOn_Off.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(myBroadcast, screenOn_Off);
}
//Inner class for the broadcast receiver, also in the service.
public class MyBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Broadcast received", "Broadcast received");
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
startTimers();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
screenLocked();
}
}
};
//Declaration of receiver in manifest.
<receiver
android:name="com.bla.bla.bla$MyBroadcastReceiver"
android:enabled="true">
</receiver>
I purposely did not include the intent filters for the screen_on and off because I read that those must be declared programmatically, including them made not difference.

Unregister Receiver is it necessary?

I have an alarm manager whose receiver I had registered in my code. The whole point of having an alarm manager in case of Timer is that it should run in background when in Pause state. Now, do I unregister it in onPause() or in OnDestroy() and will it still run in background and wake up and the receiver would receive it?
EDIT:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyClass.class);
i.putExtra("fromReciever", true);
startActivity(i);
}
I would suggest that you register it in your AndroidManifest.xml file. Example:
<receiver android:name="com.example.android.MyReceiver" >
<intent-filter>
<action android:name="com.example.android.USER_ACTION" />
</intent-filter>
</receiver>
This will keep your receiver registered as long as the application is installed on your device. All you have to do is implement it:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
And you are set.
Additionally, you can check out this tutorial for more information.
Added
If you want to resume your Activity you can add this code in your Receiver onReceive method.
Intent intent = new Intent(this, YourActivity.class);
intent.putExtra("fromOnReceive", true);
context.startActivity(intent);
Then, in your Activity onCreate method, you check if it was called from your Receiver
#Override
protected void onCreate(Bundle savedInstanceState) {
if(getIntent().hasExtras()){
boolean fromReceiver = getIntent().getExtras().getBoolean("fromOnReceive");
if(fromReceiver)
//Do work
}
}

Activity cannot receive intent using intent filter and self defined broadcast receiver even the self defined action matches

In a class I want to send an intent to my Activity:
Intent broadcast = new Intent();
broadcast.setAction("coinchutc.ANNONCE");
Log.d("JoueurAgent", "Sending broadcast " + broadcast.getAction());
context.sendBroadcast(broadcast);
The Log can print out message correctly.
In my Activity class I declared a self-defined broadcast receiver:
myReceiver = new MyReceiver();
The class MyReceiver is like this:
private class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("PartieActivity", "receive");
String action = intent.getAction();
if (action.equalsIgnoreCase("coinchutc.ANNONCE")) {
Log.d("PartieActivity", "Receive " + action);
annoncer();
}
}
}
I've registered my activity like this in the onCreate() method of the class:
IntentFilter annonceFilter = new IntentFilter();
annonceFilter.addAction("coinchutc.ANNONCE");
registerReceiver(myReceiver, annonceFilter);
But the Log in the MyReceiver class doesn't print anything at all.
Does anyone know any other possible reason why it is like this?
Thanks in advance!
Have you this in you Manifest?
<receiver android:name="xxx.MyReceiver">
<intent-filter>
<action android:name="coinchutc.ANNONCE" />
</intent-filter>
</receiver>
I also encountered such a problem. And I tried to change the action name, then the receiver can receive the intent. I don't know why, but you can have a try.

Filter Intent based on custom data

I want to broadcast intent with custom-data and only the receiver that have this custom data should receive this intent, how this could be done ?
this how i broadcast the intent :
Intent intent = new Intent();
intent.setAction("com.example");
context.sendBroadcast(intent);
and i define the broadcast receiver as following :
<receiver android:name="com.test.myReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.example"></action>
</intent-filter>
</receiver>
Setup your myReceiver class basically how anmustangs posted.
public class myReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent intent) {
//Do something
}
}
You shouldn't have to check the Intent action because you've already filtered for it based on what you've included in your manifest, at least in this simple case. You could have other actions and filters in which case you'd need to have some kind of check on the received intent.
If you want the filter to filter on data, the sdk documentation on intent filters covers that. I'm not great with data types so any example I would give would be relatively poor. In any event, here is a link to the manifest intent filter page:
http://developer.android.com/guide/topics/manifest/intent-filter-element.html
And the specific page for the data element;
http://developer.android.com/guide/topics/manifest/data-element.html
If you already define your broadcast receiver class, then the next step is to register it on the Activity/Service which you'd like to receive the broadcast intent. An example to register:
IntentFilter iFilter = new IntentFilter();
iFilter.addAction("com.example"); //add your custom intent to register
//below is your class which extends BroadcastReceiver, contains action
//which will be triggered when current class received the broadcast intent
MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver, iFilter); //register intent & receiver
If you destroy the activity/service, for best practice don't forget to unregister the broadcast receiver on the same activity/service:
#Override
protected void onDestroy() {
if(myReceiver!=null)
unregisterReceiver(myReceiver)
super.onDestroy();
}
MyReceiver class:
public class MyReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent intent) {
if(intent.getAction().equals("com.example")){
//DO SOMETHING HERE
}
}
}

Categories

Resources