Search code examples
javaandroidandroid-intentbroadcastreceiver

Difficulty sending data on the Broadcast receiver through Intent


I have a Touch Mobile Computer with a barcode scanner. I'm trying to write an application that scans a barcode and imports data from the DB into the device. In order to use the scanner I use a broadcast receiver. On the scan activity screen, there are a few barcodes to scan. I set the intent to transfer information from which edittext the scan was performed (using putextra). The broadcast receiver receives the scan the action but the input of the putextra is not exist (The handle variable gets a null value [Attached picture]).

I would be happy to help with what I'm doing wrong.

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

enter image description here


Solution

  • The item that you're sending in broadcast has action:

    intent.setAction(BarcodeControllerConstants.ACTION_BARCODE_OPEN);
    

    But in onReceive() you're checking different action:

    action.equals(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_DECODING_DATA)
    

    Are you sure this is the same intent that the one you're putting extras to?

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

    You don't even register BarcodeControllerConstants.ACTION_BARCODE_OPEN action in your BarcodeController broadcast, so I think it's not received.