BroadcastReceiver.onReceive呼叫多次

时间:2019-05-27 10:12:41

标签: android broadcastreceiver

我有两个版本debugrelease

这里的结构:

app/src/main/java/com/myproject/app/fragment/PaidInvoicesBaseFragment.java

摘要:

public class PaidInvoicesBaseFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

simpleCursorAdapter = new InvoiceAdapter(getActivity(), R.layout.invoice_list_item, null, dataColumnsNames,
                viewIDs, 0);
}

  @Override
    public void onResume() {
        super.onResume();     
        initLogic();
    }

    protected void initLogic() {

        IntentFilter intentFilter = new IntentFilter(NotificationsSyncModule.SYNC_ACTION);
        intentFilter.setPriority(NotificationsSyncModule.FRAGMENT_PRIORITY);

        getActivity().registerReceiver(mMessageReceiver, intentFilter);

}

@Override
    public void onPause() {
        super.onPause();
        getActivity().unregisterReceiver(mMessageReceiver);
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mMessageReceiver);
}

InvoiceAdapter位于:

app/src/main/java/com/myproject/app/widget/adapter/InvoiceAdapter.java

以下代码段:

public class InvoiceAdapter extends SimpleCursorAdapter {

imageViewLargeSendPostalOffice.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent();
                    intent.setAction(SEND_POSTAL_OFFICE);

                    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
                }
            });
}

这里是dubug版本:

/app/src/debug/java/com/myproject/app/fragment/PaidInvoicesFragment.java

摘要:

public class PaidInvoicesFragment extends PaidInvoicesBaseFragment {

    @Override
    protected void initLogic() {
        IntentFilter intentFilterSendPostalOffice = new IntentFilter(InvoiceAdapter.SEND_POSTAL_OFFICE);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mMessageReceiver,
                intentFilterSendPostalOffice);

        super.initLogic();
    }

    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(InvoiceAdapter.SEND_POSTAL_OFFICE)) {
                showConfirmDialogSendPostalOffice();
            }
        }
    };

当我在imageViewLargeSendPostalOffice中单击InvoiceAdapter时,它将发送广播SEND_POSTAL_OFFICE。 结果PaidInvoicesFragment收到此广播。

很好。它工作正常……除了一个例外。

显示PaidInvoicesFragment时,我将应用程序最小化,然后再次返回到应用程序,然后单击imageViewLargeSendPostalOffice方法

LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

仅呼叫一次,但在PaidInvoicesFragment onReceive(Context context, Intent intent)中呼叫两次

如果我最小化应用2次,然后onReceive(Context context, Intent intent)调用4次。 如果我最小化应用3次,然后onReceive(Context context, Intent intent)调用6次。 依此类推。

但是我需要onReceive(Context context, Intent intent)只打一次电话。

0 个答案:

没有答案