i want when a new message received by onMessage() inside GCMIntentService.java , the onMessage() call a method called blinkLED() inside the MainActivity.java so the blinkLED() method can use the data received by onMessage() , how can i implement that ? a sample code will be helpful.
Use BroadcastReceiver. This tutorial shows you how to send a broadcast intent from a class, and have another class handles it.
Short example, in your GCMIntentService::onMessage(), you may have this:
Intent intent = new Intent();
intent.setAction("com.my.app.blinkled");
sendBroadcast(intent);
Then in your MainActivity, you implements a BroadcastReceiver :
private class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
blinkLED();
}
}
and register for it in onResume() of MainActivity:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.my.app.blinkled");
receiver = new MyBroadcastReceiver();
registerReceiver(receiver, intentFilter);
Related
Let's say I have this receiver:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(ACTION.equals(intent.action)){
doSth()
}
};
I then register it dynamically like that:
mContext.registerReceiver(mReceiver, new IntentFilter(ACTION));
Do I need to check inside the receiver with
if(ACTION.equals(intent.action)){
doSth()
}
since inside the method registerReceiver I put an intentFilter with ACTION?
You do not need to check for action assuming your filter is correct. As per documentation, "The receiver will be called with any broadcast Intent that matches filter, in the main application thread". More info: https://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver,%20android.content.IntentFilter)
I am trying to start an intent service from one activity which gets data from server and try to get that data in another activity using broadcast receiver but the broadcast receiver class is not getting called, I have registered my service class in manifest also
Here is my code to start a service
Intent intent = new Intent(LoginActivity.this, MyWebRequestService.class);
startService(intent);
Class MyWebRequestService
//inside on handle intent
new AsynchCall().execute();
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MyWebRequestReceiver.PROCESS_RESPONSE);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
//here I am getting the data in my arraylist
broadcastIntent.putStringArrayListExtra("namelist", servicelist);
sendBroadcast(broadcastIntent);
Activity where I want to receive my data
IntentFilter filter =IntentFilter(MyWebRequestReceiver.PROCESS_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MyWebRequestReceiver();
registerReceiver(receiver, filter);
In the same activity I have written my broadcast receiver class
public class MyWebRequestReceiver extends BroadcastReceiver
{
public static final String PROCESS_RESPONS = "com.rentpro.PROCESS_RESPONSE";
#Override
public void onReceive(Context context, Intent intent)
{
listofnames = intent.getStringArrayListExtra("namelist");
}
}
Make sure that the receiver
registerReceiver(receiver, filter);
is called before sending the broadcast
sendBroadcast(broadcastIntent);
I am trying to use BroadcastReceiver inside my service but it is not working properly.
I am starting my service in an onCreate in my activity. Then in the services onCreate I am calling the following to register the Broadcast reciever:
IntentFilter filter = new IntentFilter();
registerReceiver(DataUpdateReceiver, filter);
Here's the broadcast receiver i am trying to register:
private BroadcastReceiver DataUpdateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();
}
};
Then else where in the Activity I am trying to call this so therefor the Toast message will be displayed.
Intent i = new Intent();
sendBroadcast(i);
But the Toast is not being displayed, I have also tried logging but nothing shows up. If anyone could help me out on this it would be appreciated, ty.
In my opinion, you have to specify action (or actions), which fire onReceive() method. Something like this might help you:
IntentFilter filter = new IntentFilter("some_action");
registerReceiver(DataUpdateReceiver, filter);
...
Intent i = new Intent("some_action");
sendBroadcast(i);
Declare on top of the class
public final static String MY_RECEIVER_START = "com.yourcompanyname.appname.MY_RECEIVER_START";
private Radio radio;
In the service constructor
//Initiate our receiver
radio = new Radio();
//Activate our recevier
context.registerReceiver(radio, new IntentFilter(MY_RECEIVER_START));
Also in the service, create the receiver class and the method which shows toast
/**
* Receiver Class
* This setup checks for the incoming intent action to be able to
* attach more messages to one receiver.
*/
private class Radio extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MY_RECEIVER_START)){
//show toast
}
}
}
After from anywhere in the application send message to our radio
context.sendBroadcast(new Intent("com.yourcompanyname.appname.MY_RECEIVER_START"));
I send a Broadcast by doing:
Intent intent = new Intent("com.usmaan.myApp.DATA_RECEIVED");
intent.putExtra("matchId", newRowId);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
This is the Service that wraps up the AsyncTask which runs Broadcasts the above:
<service
android:name=".services.DataService"
android:exported="false" />
In my Activity, I register a Receiver in onResume:
IntentFilter intentFilter = new IntentFilter("com.usmaan.myApp.DATA_RECEIVED");
registerReceiver(mDataReceiver, intentFilter);
The `BroadReceiver looks like this:
private class DataReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final long matchId = intent.getLongExtra("matchId", 0);
Toast.makeText(LaunchActivity.this, "" + matchId, Toast.LENGTH_LONG);
}
}
The onReceive is never fired. What am I doing wrong?
Either use LocalBroadcastManager in both places (sendBroadcast() and registerReceiver()), or do not use LocalBroadcastManager at all. Right now, you have a mismatched pair.
I have a class that extends BroadcastReceiver that reads new sms
public class SmsReceiver extends BroadcastReceiver
{
// reading sms
// I want to send the sms text to my main activity
}
And have another class in the same app that is my main Activity.
So when I receive new sms, I want to send its content to my main Activity that is already running and display it.
How can I do that?
I would be thankful for some code samples :)
i can suggest you two possibilities
send new broadcasts from this receiver to a new receiver which is registered inside your activity
register this receiver inside your activity and reduce the hassle
i guess option two is more suitable
this is how you may register a broadcast receiver inside your activity class:
IntentFilter filter = new IntentFilter();
public void onResume(){
filter.addAction("action_string_1");
filter.addAction("action_string_2");
registerReceiver(receiver, filter);
}
public void onPause(){
unregisterReceiver(receiver);
}
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("action_string_1")){
//do something here
}
else if(action.equals("action_string_2")){
//do somethign here
}
}
};