Broadcast receiver is triggered automatically on start

梦想与她 提交于 2019-12-04 04:48:50

问题


I have a registered BroadcastReceiver in my main activity. Activity sends a sticky in one of the tabs to trigger the broadcast receiver (TabActivity application).

Everything works fine, but when I restart the app the sticky is sent automatically (not triggered by user) and view is opened.

My question is: how is that possible? Did I misunderstand something? And how can I fix that?

MainActivity: OnCreate:

    registerReceiver(openOutgoingCall, new IntentFilter("OPENOUTGOINGCALL"));

BroadcastReceiver:

private BroadcastReceiver openOutgoingCall = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            Bundle extras = intent.getExtras();
            if(extras.isEmpty() == false) {
                HashMap<String,String> callData = (HashMap<String, String>) extras.get("callData");
                openOutgoingCall(callData); 
            }

        }
    };

Activity inside TabHost

public void openCall(View view) {
    Intent i = new Intent("OPENOUTGOINGCALL");
    i.putExtra("callData", detailInfo);
    sendStickyBroadcast(i);    
}

回答1:


Sticky broadcasts are supposed to stay around (even they are received) so that they can be retrieved afterwards too. Perhaps you should try the simple way of broadcasting using:

sendBroadcast(i);

Read this.



来源:https://stackoverflow.com/questions/12490081/broadcast-receiver-is-triggered-automatically-on-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!