Search code examples
androidandroid-intentandroid-broadcastreceiver

where does the BroadcastReceiver intent come from? and how can I change it?


in the app I'm working on, whenever the user receives a message, the message contents get delivered to the app using a BroadcastReceiver. I want to add a field to this intent and get some extra information, just don't know where does the intent come from.

Here is the broadcast and its onReceive() method:

br = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {

            int task = intent.getIntExtra(PARAM_TASK, 0);
            int status = intent.getIntExtra(PARAM_STATUS, 0);

            int msgId = intent.getIntExtra("msgId", 0);
            long msgFromUserId = intent.getLongExtra("msgFromUserId", 0);
            int msgFromUserState = intent.getIntExtra("msgFromUserState", 0);
            int msgFromUserVerify = intent.getIntExtra("msgFromUserVerify", 0);
            String msgFromUserUsername = intent.getStringExtra("msgFromUserUsername");
            String msgFromUserFullname = intent.getStringExtra("msgFromUserFullname");
            String msgFromUserPhotoUrl = intent.getStringExtra("msgFromUserPhotoUrl");
            String msgMessage = intent.getStringExtra("msgMessage");
            String msgImgUrl = intent.getStringExtra("msgImgUrl");
            int msgCreateAt = intent.getIntExtra("msgCreateAt", 0);
            String msgDate = intent.getStringExtra("msgDate");
            String msgTimeAgo = intent.getStringExtra("msgTimeAgo");
            int msgTransactionValue = intent.getIntExtra("transValue",0);

            Chat1Item c = new Chat1Item();
            c.setId(msgId);
            c.setFromUserId(msgFromUserId);
            c.setFromUserState(msgFromUserState);
            c.setFromUserVerify(msgFromUserVerify);
            c.setFromUserUsername(msgFromUserUsername);
            c.setFromUserFullname(msgFromUserFullname);
            c.setFromUserPhotoUrl(msgFromUserPhotoUrl);
            c.setMessage(msgMessage);
            c.setImgUrl(msgImgUrl);
            c.setCreateAt(msgCreateAt);
            c.setDate(msgDate);
            c.setTimeAgo(msgTimeAgo);
            c.setTransactionValue(msgTransactionValue);

Log.e(LOG_TAG, "onReceive: task = " + task + ", status = " + status + " " + c.getMessage() + " " + Integer.toString(c.getId()));



            final Chat1Item lastItem = (Chat1Item) 
listView.getAdapter().getItem(listView.getAdapter().getCount() - 1);

            messagesCount = messagesCount + 1;

            chat1List.add(c);

            if (!visible) {

                try {

                    Uri notification = 
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                    Ringtone r = RingtoneManager.getRingtone(getActivity(), notification);
                    r.play();

                } catch (Exception e) {

                    e.printStackTrace();
                }
            }

            chatAdapter.notifyDataSetChanged();

            scrollListViewToBottom();



            getNextMessages();
        }

it also uses an IntentFilter to register the BroadcastReceiver:

    IntentFilter intFilt = new IntentFilter(BROADCAST_ACTION);
    getActivity().registerReceiver(br, intFilt);

also these are the intent-filters that are in my manifest file:

       <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


        <service
            android:name="com.thetasocial.app.service.MyFcmListenerService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <service
            android:name="com.thetasocial.app.service.MyInstanceIDListenerService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>

        <service
            android:name="com.thetasocial.app.service.MyService"
            android:exported="false" />

give me a clue to start from. thanks in advance.


Solution

  • First of all this is local broadcast receiver.i.e., Anonymous class so you cannot find that receiver in the manifest.xml. This receiver has to be registered and unregistered which is mandatory and that too it has to be done in that particular activity (or) fragment, in that registering (or) unregister code we can find the broadcast intent filter name. In your case BROADCAST_ACTION is the name. Try to find the usage of the BROADCAST_ACTION in the whole project. To find the usage in whole project, if you are using Android studio means shortcut is ctrl+shift+F. Your broadcast intent will be like below,

     Intent intent = new Intent(BROADCAST_ACTION);
    
    sendBroadcast(intent);
    

    Note the intent filter name is just a string BROADCAST_ACTION should a constant value declared somewhere.

    Hope this is helpful:)