BroadcastReceiver 正在接收 10 个意图中的 1 个

BroadcastReceiver is receiving like 1 of 10 intents

单击按钮时,我将打开 activity(ActionListActivity) 并将意图发送到 IntentService(稍后该服务将广播意图发送到 ActionListActivity)。但我通常在发布后只收到第一个意图。在接收者注册之前发送意图是真的吗?

我想通过 intentService 获取数据,并使用它更新我的 UI。

方案Activity->IntentService->BroadCastReceiver inside ActionListActivity

Activity:

private void selectDrawerItem(MenuItem menuItem)
    {
        switch (menuItem.getItemId()) {
            case R.id.actions:{
                Intent myIntent = new Intent(this, ActionListActivity.class);
                this.startActivity(myIntent);
                ProgressDialog progressDialog = new ProgressDialog(this);
                progressDialog.show();
                Intent dataIntent = new Intent(this, DatabaseWorkIntentService.class);
                dataIntent.putExtra(Utils.INTENT_SERVICE_INVOKE, Utils.READ_ACTIONS_DATA);
                startService(dataIntent);
                progressDialog.dismiss();
            }
        }
    }

意图服务:

     private void readActionData(){
            Log.e("read actions data","data");
            List<Action> actionList;
            actionList = Action.listAll(Action.class);

            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction(Utils.READ_ACTIONS_DATA);
            broadcastIntent.putParcelableArrayListExtra(Utils.READ_ACTIONS_DATA, (ArrayList<? extends Parcelable>) actionList);
            sendBroadcast(broadcastIntent);
        }

动作列表Activity:

public class ActionListActivity extends BaseActivity {

    boolean mIsReceiverRegistered = false;
    DataBroadcastReceiver receiver;
    TextView someTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.action_list_activity);
        this.initToolbarAndDrawerWithReadableName(getString(R.string.our_actions));

        someTv = (TextView)findViewById(R.id.someTv);
    }

    public void someTvTest(Action action){
        someTv.append(action.getName());
        Log.e("data",action.getName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mIsReceiverRegistered) {
            unregisterReceiver(receiver);
            receiver = null;
            mIsReceiverRegistered = false;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (!mIsReceiverRegistered) {
            if (receiver == null)
                receiver = new DataBroadcastReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction(Utils.READ_ACTIONS_DATA);
            receiver.setMainActivityHandler(this);
            registerReceiver(receiver, filter);
            mIsReceiverRegistered = true;
        }
    }
}
class DataBroadcastReceiver extends BroadcastReceiver{

    ActionListActivity activity = null;
    ArrayList<Action> list;

    public void setMainActivityHandler(ActionListActivity main){
        activity = main;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("reciever","reciev");
        list = intent.getParcelableArrayListExtra(Utils.READ_ACTIONS_DATA);
        for (Action action:list){
            if(activity!=null) {
                activity.someTvTest(action);
            }
        }
    }
}

您启动 activity 和服务异步。 您需要在 ActionListActivity 中启动服务并等待响应。