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);
Related
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);
Is there anyway to register a BroadcastReceiver in AndroidManifest.xml and receives broadcast that is send by a LocalBroadcastManager?
Currently I must call
registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
to register a Receiver, declare in AndroidManifest.xml won't work. But this means I must know exactly the receiver's package name and class name, not just the intent filter. Is it possible to declare the receiver in the manifest file?
following is my current code.
AndroidManifest.xml:
...
<receiver
android:name="com.example.test.MessageReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="com.m2x.test.intent.MESSAGE_RECEIVED" />
</intent-filter>
</receiver>
...
MainActivity.java:
Intent intent = new Intent();
intent.setAction("com.m2x.test.intent.MESSAGE_RECEIVED");
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mContext.get());
manager.sendBroadcast(intent);
MessageReceiver.java
public class MessageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.m2x.test.intent.MESSAGE_RECEIVED")) {
Toast.makeText(context, "user message received", Toast.LENGTH_SHORT).show();
}
}
}
No, you can't.
The local BroadcastReceiver isn't a real BroadcastReceiver, basically its a list of callbacks functions.
You can check the source code of LocalBroadcastManager.java.
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.
I have a class that extends the broadcast receiver. My question is how will I go about calling on this activity in another class... I tried to create a intent for it but I kept getting a syntax error. Is it another way to start the broadcast receiver?
You have to use
Intent intent = new Intent( "mypackage.myaction" );
activity.sendBroadCast( intent );
Where activity is the one that launches your BroadcastReceiver and Intent, an intent that matches the filter of your BroadcastReceiver in your manifest file.
You will do something that looks like :
<receiver android:name="your broadcast receiver class" android:label="a name">
<intent-filter>
<action android:name="mypackage.myaction" />
</intent-filter>
</receiver>
Regards,
Stéphane
IntentFilter filter = new IntentFilter("com.mydefinepackage.myactivity");
this.registerReceiver(new Receiver(), filter);
Declare this private class and use above code within myactivity Activity.
private class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
myOwnMethod();
}
}
Execute this code from any other activity. myOwnMethod will be called then.
Intent i =new Intent("com.mydefinepackage.myactivity");
sendBroadcast(i);
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
}
}
}