Android - BroadcastReceiver not calling - android

when i was register the broadcast receiver, onReceive was not called. here is my code
#Override
public void onUserInteraction() {
super.onUserInteraction();
IntentFilter filter = new IntentFilter();
registerReceiver(mBroadcastReceiver, filter);
}
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.v("sunil", "broadcastreceiver======");
}
};

you have to look for a string just to tell the android what are you trying to looking for , for example if you want to try some code on on receive you should use a string in you intentfiltre
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");

Related

Broadcast receiver in fragment called 3 time send only once

I am creating sending broadcast from activity to fragment. But in my case receiver called three times as I only send broadcast once. please help
Here , I am registering broadcast in Fragment
#Override
public void onResume() {
super.onResume();
getActivity().registerReceiver(broadcastReceiver, new IntentFilter("example.com"));
}
#Override
public void onPause() {
super.onPause();
getActivity().unregisterReceiver(broadcastReceiver);
}
And my receiver in Fragment
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Here I am receiving the data and want to update the list in
//my fragment
};
Sending the broadcast
Intent intent = new Intent("example.com");
intent.putExtra("data","any_data");
sendBroadcast(intent);
You must always set Action to Intent like below,
Intent intent = new Intent();
intent.setAction("action_type");
sendBroadcast(intent);
And in your receiver add this line :
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction("action_type")) {
// Write your code here now.
}
};
Hope it works.

Broadcast receiver in Fragment?

I use broadcast receiver, and in Fragment.java i register. But it's not working.
My code:
Intent intent = new Intent("android.intent.action.LOGIN");
intent.putExtra("message", obj.getToken());
intent.setAction("com.sunrise.android.LOGIN");
loginView.getContext().sendBroadcast(intent);
In Fragment:
#Override
public void onResume() {
super.onResume();
informationStudentPresenter = new InformationStudentPresenter(this);
IntentFilter intentFilter = new IntentFilter("android.intent.action.LOGIN");
getActivity().registerReceiver(broadcastReceiver, intentFilter);
}
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getActivity(), "CLGT", Toast.LENGTH_SHORT);
if(intent.getAction().equalsIgnoreCase("com.sunrise.android.LOGIN")){
if(!"".equals(findToken())) {
informationStudentPresenter.getProfileStudent();
informationStudentPresenter.showToast();
}
}
}
};
Could you tell me where my mistake and how to fix it?
Thanks.
You're changing the Intent's action name right after initializing it with a different name. Then you're registering the broadcast receiver with an IntentFilter that waits for the old action name.

Register a broadcast receiver in a Service

I have an Activity, this Activity will call a Service and that Service will call a broadcast to listen for SMS coming. I register a broadcast in the Service dynamically.
But it's not working. My code is below. Please help me resolve.
This is the onCreate method in the Service.
#Override
public void onCreate() {
super.onCreate();
final IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
this.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
showSuccessfulBroadcast();
}
}, filter);
}
The code is correct. Don't forget to declare used permission in AndroidManifest.xml, and to unregister receiver in onDestroy(). Both points are important.
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
Inside your Service
private BroadcastReceiver mSmsBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
showSuccessfulBroadcast();
}
};
#Override
public void onCreate() {
super.onCreate();
final IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
this.registerReceiver(mSmsBroadcastReceiver, filter);
}
#Override
public void onDestroy() {
this.unregisterReceiver(mSmsBroadcastReceiver);
}

LocalBroadcastManager don't receive broadcast on 2.3

Not works on 2.3. But works on 4.0.x. Why?
Sending from IntentService:
intent.setAction(MessagesThread.NEW_MESSAGE);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Receiving
protected void onStart() {
super.onStart();
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,new IntentFilter(NEW_MESSAGE));
}
protected void onStop() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onStop();
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
.......................................
.......................................
}
};
I am not sure, but the issue might be due to LocalBroadcastManager. Try sending broadcast without using LocalBroadcastManager, like below:
SEND BROADCAST
Intent i = new Intent("ALERT_CHANGE");
i.putExtra("DATA","News");
sendBroadcast(i);
RECEIVE BROADCAST-in Activity
registerReceiver(uiUpdated, new IntentFilter("ALERT_CHANGE"));
private BroadcastReceiver uiUpdated= new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
String DATA = i.getStringExtra("Data");
txt.settext(DATA);
}
};
LocalBroadcastManager.getInstance(this).sendBroadcast(newIntent(MessagesThread.NEW_MESSAGE).putExtras(intent));

BroadcastReceiver is not receiving broadcast intents

I need to send broadcast intent for specific receiver.
I use this function for it:
void sendTestBroadcast(Class<? extends BroadcastReceiver> clazz) {
Intent intent = new Intent(context, clazz);
intent.setAction(MY_ACTION);
context.sendBroadcast(intent);
}
Then I handle results in my Activity:
#Override
protected void onStart() {
super.onStart();
mReceiver = new Receiver();
registerReceiver(mReceiver, new IntentFilter(MY_ACTION));
}
#Override
protected void onStop() {
super.onStop();
unregisterReceiver(mReceiver);
mReceiver = null;
}
Receiver is private and non-static class. I pass Receiver.class to my receiver sender as an argument. I also tried to use ComponentName to set destanation target, but it's still not working. I even tried to do Receiver class static -- same result.
What am I doing wrong?

Categories

Resources