Reputation: 5150
I have an activity and and a service. I am running my service in background in a time interval through AlaramManager. What I want is to receive periodically data from the service inside activity. For this I'm using broadcastreceiver, but it does not showing any data.
In my service I'm using this method for sending data:
private final void sendServiceActiveBroadcast(final boolean pActivate) {
final Intent _intent = new Intent();
_intent.setAction(BROADCAST_ACTION);
_intent.addCategory("com.monday.worker_android.android.CATEGORY");
_intent.putExtra("isactive", pActivate);
NewService.this.sendBroadcast(_intent);
}
And use it inside an AsyncTask class like:
@Override
protected void onPostExecute(String result) {
Log.d("Post Execute", "Executed");
super.onPostExecute(result);
float[] arr = new float[30];
if (round(distance(LATITUDE, LONGITUDE, lati, longi)) < 200) {
Log.d("OnPostExecute", "In");
sendServiceActiveBroadcast(true);
}
}
And try to receive this in my activity like:
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean value = intent.getBooleanExtra("isactive", false);
if (value == true) {
Toast.makeText(getApplicationContext(), "received",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), " not received",
Toast.LENGTH_SHORT).show();
}
}
};
I resister it in my onResume() and unresister it in my onPause() like:
@Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(NewService.BROADCAST_ACTION);
registerReceiver(receiver, filter);
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
I just check some tutorials and code it. My application is working fine with service and also it excutes the onPostExecute() perfectly. But it does't show any broadcast data.
Can any one please suggest me how to receive periodically data from service and why I fail to receive data here and about my mistakes.
Thank You
Upvotes: 0
Views: 423
Reputation: 22064
addCategory
is the problem in your code. Because, in your activity you didn't set the category
attribute, so the reference can not be found. Also, without having the category you can send this to multiple receivers at different locations with action
attribute.
Upvotes: 1