难以通过 Intent 在广播接收器上发送数据

Difficulty sending data on the Broadcast receiver through Intent

我有一台带条码扫描仪的触控式移动数据终端。 我正在尝试编写一个扫描条形码并将数据从数据库导入设备的应用程序。为了使用扫描仪,我使用了广播接收器。 在扫描 activity 屏幕上,有一些条形码需要扫描。我设置了传输执行扫描的 edittext 的信息的意图(使用 putextra)。广播接收器接收到扫描动作但是putextra的输入不存在(句柄变量得到一个空值[附图])。

我很乐意帮助解决我做错的事情。

Activity Class:

                        public class MoveItemActivity extends AppCompatActivity {
                    private EditText item;
                    private EditText to;
private boolean mIsRegisterReceiver = false;
    private BroadcastReceiver mBarcodeReceiver;

                @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_move_item);
                item = (EditText) this.findViewById(R.id.itemInEditText);
                        item.setOnFocusChangeListener(mEditText);
                        to = (EditText) this.findViewById(R.id.toInEditText);
                        to.setOnFocusChangeListener(mEditText);
            this.registerReceiver();
            }

            EditText.OnFocusChangeListener mEditText = new EditText.OnFocusChangeListener(){
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    Intent intent = new Intent();
                    switch (v.getId()){
                        case R.id.itemInEditText:
                            if (!hasFocus){
                                new CheckItem(MoveItemActivity.this).execute(item.getText().toString());
                                break;
                            }
                            else {
        intent.setAction(BarcodeControllerConstants.ACTION_BARCODE_OPEN);
                                intent.putExtra(BarcodeControllerConstants.EXTRA_HANDLE, "item");
                                intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                sendBroadcast(intent);
                                break;
                            }
                        case R.id.toInEditText:
                            if (!hasFocus){
                                new CheckLocation().execute(to.getText().toString());
                                break;
                            }
                            else
                            {
           intent.setAction(BarcodeControllerConstants.ACTION_BARCODE_OPEN);
                                intent.putExtra(BarcodeControllerConstants.EXTRA_HANDLE, "to");
                                intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                sendBroadcast(intent);
                                break;
                            }
                    }
                }
            };

       private void registerReceiver() {
            if (mIsRegisterReceiver)
                return;
            IntentFilter filter = new IntentFilter();
            filter.addAction(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_DECODING_DATA);
        filter.addAction(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_REQUEST_SUCCESS);
        filter.addAction(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_REQUEST_FAILED);
        filter.addAction(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_GET_STATUS);
            mBarcodeReceiver = new BarcodeController();
            registerReceiver(mBarcodeReceiver, filter);
            mIsRegisterReceiver = true;
           }

BroadcastReceiver Class:

public class BarcodeController extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        int mBarcodeHandle = -1;
        if (action.equals(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_DECODING_DATA)) {
            String handle = intent.getExtras().getString(BarcodeControllerConstants.EXTRA_HANDLE);
            byte[] data = intent.getByteArrayExtra(BarcodeControllerConstants.EXTRA_BARCODE_DECODING_DATA);
            String result = null;
            if (data != null) {
                result = new String(data);
                Intent i = new Intent(context, MoveItemActivity.class);
                i.putExtra(handle,result );
                context.startActivity(i);
            }
        } else if (action.equals(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_REQUEST_SUCCESS)) {
            mBarcodeHandle = intent.getIntExtra(BarcodeControllerConstants.EXTRA_HANDLE, 0);
            System.out.println("ACTION_BARCODE_CALLBACK_REQUEST_SUCCESS:" + mBarcodeHandle);
        } else if (action.equals(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_REQUEST_FAILED)) {
            int result = intent.getIntExtra(BarcodeControllerConstants.EXTRA_INT_DATA2, 0);
            System.out.println("ACTION_BARCODE_CALLBACK_REQUEST_FAILED:" + result);
        } else if (action.equals(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_GET_STATUS)) {
            int status = intent.getIntExtra(BarcodeControllerConstants.EXTRA_INT_DATA2, 0);
            System.out.println("ACTION_BARCODE_CALLBACK_GET_STATUS:" + status);
        }
    }
}

您在广播中发送的项目具有操作:

intent.setAction(BarcodeControllerConstants.ACTION_BARCODE_OPEN);

但在 onReceive() 中,您正在检查不同的操作:

action.equals(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_DECODING_DATA)

您确定这与您添加额外内容的意图相同吗?

intent.putExtra(BarcodeControllerConstants.EXTRA_HANDLE, "item");

你甚至没有在你的 BarcodeController 广播中注册 BarcodeControllerConstants.ACTION_BARCODE_OPEN 操作,所以我认为它没有收到。