BroadcastReceiver意图来自哪里?以及如何更改它?

时间:2017-08-08 14:43:07

标签: android android-intent android-broadcastreceiver

在我正在处理的应用中,每当用户收到消息时,消息内容都会使用BroadcastReceiver传递到应用。我想为这个意图添加一个字段并获得一些额外的信息,只是不知道意图来自何处。

这是广播及其onReceive()方法:

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();
        }

它还使用IntentFilter注册BroadcastReceiver

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

这些也是我的清单文件中的intent-filters:

       <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" />

给我一​​个从头开始的线索。提前谢谢。

2 个答案:

答案 0 :(得分:0)

找到sendBroadcast文件或项目......在那里你会看到BroadcastReciever广播的意图和意图内容......

答案 1 :(得分:0)

首先,这是本地广播接收器。即,匿名类,因此您无法在manifest.xml中找到该接收器。该接收器必须是注册和未注册的,这是强制性的,并且必须在该特定活动(或)片段中完成,在注册(或)注销代码中,我们可以找到广播意图过滤器名称。在您的情况下,BROADCAST_ACTION是名称。尝试在整个项目中查找BROADCAST_ACTION的用法。要查找整个项目的用法,如果您使用的是Android工作室,则快捷方式为ctrl + shift + F。您的广播意图如下所示,

 Intent intent = new Intent(BROADCAST_ACTION);

sendBroadcast(intent);

注意,如果在某处声明了常量值,则intent过滤器名称只是一个字符串BROADCAST_ACTION

希望这有用:)