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 :)
}
}
};
Related
I want the service to perform a stopForeground and a stopSelf after the notification is clicked followed by the running of pendingIntent.
I have tried using a BroadcastReceiver which is never called as I checked during debugging. I have added it to manifest as well.
Intent intentHide = new Intent(this, StopServiceReceiver.class);
PendingIntent hide = PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), intentHide, PendingIntent.FLAG_CANCEL_CURRENT);
Added it to the builder
builder.setContentIntent(hide);
And the Broadcast Rec is done separately -
public class StopServiceReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 333;
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, TimerService.class);
context.stopService(service);
}
}
Manifest -
<receiver
android:name=".StopServiceReceiver"
android:enabled="true"
android:process=":remote" />
This is not working. The notification and the service both are alive.
Questions - Should I use addContent instead of setContentIntent ? If yes, then what should the parameters be ?
Is there anything I went wrong with? What could possibly be wrong with such kind of implementation? Thank you.
I had the same problem in the notification.
This code is working perfectly.
void creatnotifiaction()
{
public static final String STOP = "com.example.android";
public static final int REQUEST_CODE = 333;
filter = new IntentFilter();
filter.addAction(STOP);
Intent intentHide = new Intent(STOP);
PendingIntent hide = PendingIntent.getBroadcast(this,REQUEST_CODE,intentHide,PendingIntent.FLAG_CANCEL_CURRENT);
registerReceiver(broadcastReceiver, filter);
}
There no need to separate broadcast receiver use in same class.
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#SuppressLint("ResourceAsColor")
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.d("notification", "Received intent with action " + action);
switch (action) {
case STOP:
//your code to stop notifications or service.
break;
}
});
Let me know if that work for you.
Thanks...Happy coding.
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);
So i was learning about BroadcastReceiver in android. BroadcastReceivers are based on observer design patterns (which makes them very interesting).
So what i am trying to achieve is to broadcast my message and inside same activity i want to receive the message (Just for testing purpose).
Here is the code snippet
Log.d("ABC", "sending");
Intent intent = new Intent("com.yourcompany.testIntent");
intent.putExtra("value", "test");
sendBroadcast(intent);
Log.d("ABC", "sent");
IntentFilter filter = new IntentFilter("com.yourcompany.testIntent");
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("ABC","INSIDE ");
String value = intent.getExtras().getString("value");
}
};
Log.d("ABC", "receiving");
registerReceiver(receiver, filter);
Log.d("ABC", "received");
Here is the debugged output
04-06 20:40:52.446
20149-20149/com.example.illuminati.broadcastreceiver D/ABC: sending
04-06 20:40:52.447
20149-20149/com.example.illuminati.broadcastreceiver D/ABC: sent 04-06
20:40:52.448 20149-20149/com.example.illuminati.broadcastreceiver
D/ABC: receiving 04-06 20:40:52.453
20149-20149/com.example.illuminati.broadcastreceiver D/ABC:received
Why it is not getting inside onReceive() method
What am i missing here
Any help?
You are sending your broadcast message before registering broadcast receiver. Try doing in the following way and it will work:
IntentFilter filter = new IntentFilter("com.yourcompany.testIntent");
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("ABC", "INSIDE ");
String value = intent.getExtras().getString("value");
}
};
registerReceiver(receiver, filter);
Intent intent = new Intent("com.yourcompany.testIntent");
intent.putExtra("value", "test");
sendBroadcast(intent);
Also you should consider using the LocalBroadcastManager if you want to broadcast a message inside your app (and not to every apps installed). See https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html
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.