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"));
Related
I have a service that subscribes from the server by Mqtt client. when arrived a message I do broadcast message and topic. In my fragment I declare a broadcast receiver like this:
private final BroadcastReceiver mChatReceiver = new BroadcastReceiver() {
int areaCode;
private BroadcastReceiver init(int areaCode) {
Log.i("====>", "init: BroadcastReceiver ");
this.areaCode = areaCode;
return this;
}
#Override
public void onReceive(Context context, Intent intent) {
Log.i("====>", "onReceive: BroadcastReceiver ");
//do sth
}
}.init(areaCode);
but init(areaCode) do not work and in original areaCode is for example 2 but I did not get 2 in private BroadcastReceiver init(int areaCode). I got 0.
how can I pass an integer out of private final BroadcastReceiver mChatReceiver class to this class?
Say you are registering for some custom IntentFilter like this:
IntentFilter filter = new IntentFilter("com.packageName.ACTION_SEND_INTEGER");
Register receiver like this:
your_context.registerReceiver(mChatReceiver,filter);
Trigger your broadcast receiver like this and send an integer:
Intent intent = new Intent("com.packageName.ACTION_SEND_INTEGER");
intent.putExtra("your_value",123);
sendBroadcast(intent);
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'm stuck here at my previous struggle >> Prev. Struggle!
Raanan there helped! me a lot but then he I think went away as timing zone is different , now I'm stuck with my service code that I'm using to call my BroadcastReceiver() that is in the activity! and also I'm not getting with what parameter I should load the filter.addAction(action); in place of action??
Kinldy guide me!
CODE in the Server:
Toast.makeText(Server.this, hr +" , " +min, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, andRHOME.class);
//intent.putExtra("sendMessage","1");
sendBroadcast(intent);
and CODE IN THE ACITIVITY(Broadcast Receiver)
private BroadcastReceiver ReceivefrmSERVICE = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "IN DA BroadCASTER",
Toast.LENGTH_LONG).show();
sendMessage("1");
}
};
IntentFilter filter = new IntentFilter();
You need to add these line to regiester your receiver for some action for example define a Global variable like this:
public static String NOTIFCATION_BROADCAST_ACTION = "com.your_packagename.UPDATE_NOTIFICATION_INTENT";
then register the action like this in your activity onCreate() Method.
IntentFilter filter = new IntentFilter();
filter.addAction(Global.NOTIFCATION_BROADCAST_ACTION);
registerReceiver(ReceivefrmSERVICE, filter);
Then send the broadcast from your service like this
Intent broadcast = new Intent();
broadcast.setAction(Global.NOTIFCATION_BROADCAST_ACTION);
sendBroadcast(broadcast);
Then in your broadcast Receiver filter this action like this
private BroadcastReceiver ReceivefrmSERVICE = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Global.NOTIFCATION_BROADCAST_ACTION)) {
//Do your stuff here :)
}
}
};
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
}
}
};
Hi I've been having an issue with Broadcast Receivers and passing information to another activity. I'm trying to create an application that will capture incoming SMS messages, look for a website in the text, then pop up an Alert Dialog box asking if the user wants to go to the website.
public class TextReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
// .. other code that
// sets received SMS into message
Toast toast = Toast.makeText(context,
"Received Text: " + message.getMessageBody(), Toast.LENGTH_LONG);
toast.show();
}
So that code works fine, receive a text it pops up a toast with the message. The toast is useless but it shows the receiver works. But I want to communicate with an activity to show an Alert Dialog and start up a webView. I already programmed the code that will take a string search for the website and open the webView. Is it possible to get the string from the broadcast receiver and do something like this?:
public class ReceiveText extends Activity{
public void onCreate(Bundle savedInstanceState) {
// Somehow pass the string from the receiver into this activity,
//stored in variable messages
findOpen(messages);
// is that possible?
}
public class findOpen(string messages){
// do stuff ... open alert...open site if OK
}
So basically I just want to pass a string from a Broadcast Receiver to another activity that will use that string. The rest of the code is basically in place all I need is that string... I'm new to this and Java and any help would be much appreciated. Thanks
Instantiate a BroadcastReceiver in the activity you want to get your data to, for example:
private BroadcastReceiver mServiceReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent)
{
//Extract your data - better to use constants...
String IncomingSms=intent.getStringExtra("incomingSms");//
String phoneNumber=intent.getStringExtra("incomingPhoneNumber");
}
};
Unregister your receiver on onPause():
#Override
protected void onPause() {
super.onPause();
try {
if(mServiceReceiver != null){
unregisterReceiver(mServiceReceiver);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Register it on onResume():
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.SmsReceiver");
registerReceiver(mServiceReceiver , filter);
}
Broadcast your data from the service via an Intent, for Example:
Intent i = new Intent("android.intent.action.SmsReceiver").putExtra("incomingSms", message);
i.putExtra("incomingPhoneNumber", phoneNumber);
context.sendBroadcast(i);
and that's it! goodLuck!
If you have your activity named ReceiveText, then in your BroadcastReceiver, you should do the following:
Intent i = new Intent(context, ReceiveText.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("message", message.getMessageBody());
context.startActivity(i);
Then, in your activity, you will need to getExtra as so:
Intent intent = getIntent();
String message = intent.getStringExtra("message");
And then you will use message as you need.
If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="#android:style/Theme.Dialog" /> in your manifest for ReceiveText and then set the message to a textview in the activity.
EDIT: This restarts your activity. this answer is likely a better solution for most people.
We can send the data from onReceive to another activity using LocalBroadcastManager.
It means you are again broadcasting the data using the context
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Broadcast", "wifi ConnectivityReceiver");
Bundle extras = intent.getExtras();
Intent intent = new Intent("broadCastName");
// Data you need to pass to another activity
intent .putExtra("message", extras.getString(Config.MESSAGE_KEY));
context.sendBroadcast(intent );
}