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);
Related
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);
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.
My question is very simple and I know the function can be preformed. I want to start activity via text message like "WHERE IS MY DROID" how can this be done? Please provide any information. I think SMSReceiver BroadcastReceiver is in the correct direction but I am not sure.
You need to register an Broadcast Receiver in AndroidManifest.xml for Receiving SMS_RECIVERD Broadcast as:
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Add permission in AndroidManifest:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
in your Broadcast Receiver code start your Application as:
public class SMSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
//context.startService(new Intent(context, YourService.class));
//Start activity as:
Intent intent24 = new Intent(Intent.ACTION_MAIN).addCategory(
Intent.CATEGORY_LAUNCHER).setClassName("YOUR_PACKAGE_NAME",
"com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("YOUR_PACKAGE_NAME",
"com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME"));
context.startActivity(intent24);
}
}
}
NOTE : For starting your Activity from background you need to set Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_FROM_BACKGROUND flags in intent for starting activity from background.
Check out this guy's sample code for intercepting SMS's http://imran-android-sms.blogspot.com/2011/03/receive-sms-on-android.html#more
From there you just want to fire off an intent to whatever your new activity should be.
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
}
}
}