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

时间:2018-03-29 13:35:48

标签: java android android-intent broadcastreceiver

我有一台带有条形码扫描仪的Touch Mobile Computer。 我正在尝试编写一个扫描条形码并将数据从数据库导入设备的应用程序。为了使用扫描仪,我使用广播接收器。 在扫描活动屏幕上,有几个条形码可供扫描。我设置了传输执行扫描的edittext信息的意图(使用putextra)。广播接收器接收扫描动作,但putextra的输入不存在(句柄变量获取空值[附图]。

我很乐意帮助解决我的错误。

活动类

                        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类

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

enter image description here

0 个答案:

没有答案