Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast receiver in a Fragment

I have a Broadcast receiver in my Fragment, to track any newly received SMS.

private BroadcastReceiver smsBroadcastReceiver;
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    smsBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("smsBroadcastReceiver", "onReceive");
        }
    };
}

@Override
public void onStart() {
super.onStart();
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(smsBroadcastReceiver, filter);
}

@Override
public void onStop() {
    super.onStop();
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(smsBroadcastReceiver);
}

and the permissions in the Manifest:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

The problem is I never get the log Log.e("smsBroadcastReceiver", "onReceive"); I definitely get the SMS while the receiver is still registered.

However if I write the receiver as a separate class and put it in the Manifest, then it's onReceive() is called.

Did I miss something?

like image 525
Housefly Avatar asked Jun 19 '15 10:06

Housefly


People also ask

Where do I register broadcast receiver in fragment?

Conclusion: So it is better to register the receiver only inside onResume() and unregister inside onPause() because onCreateView() deals with view hierarchy only. It has nothing to do with receiver.

What is broadcast receiver example?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

What are the types of broadcast receivers?

There are two types of broadcast receivers: Static receivers, which you register in the Android manifest file. Dynamic receivers, which you register using a context.

What is broadcast receiver?

Broadcast Receiver Overview. A broadcast receiver is an Android component that allows an application to respond to messages (an Android Intent ) that are broadcast by the Android operating system or by an application.


1 Answers

Did I miss something?

Yeah.. you are missing one point that objects life span. When you register the broadcast receiver in the fragment then the receiver object will alive till the fragment is exist. When the fragment is destroyed then the receiver also unregistered. So whenever the fragment is alive then only you will get the broadcast otherwise it won't work. To receive broadcast when the app is not in the foreground then you have to register the broadcast in the manifest file.

UPDATE

try like this

IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
private BroadcastReceiver smsBroadcastReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("smsBroadcastReceiver", "onReceive");
        }
    };

And register your receiver like

getActivity().registerReceiver(smsBroadcastReceiver, filter);

and unregister like

getActivity().unregisterReceiver(smsBroadcastReceiver);
like image 159
Chandrakanth Avatar answered Oct 18 '22 08:10

Chandrakanth