Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 难以通过Intent在广播接收器上发送数据_Java_Android_Android Intent_Broadcastreceiver - Fatal编程技术网

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

Java 难以通过Intent在广播接收器上发送数据,java,android,android-intent,broadcastreceiver,Java,Android,Android Intent,Broadcastreceiver,我有一台带条形码扫描仪的触摸式移动电脑。 我正在尝试编写一个应用程序来扫描条形码并将数据从数据库导入设备。为了使用扫描仪,我使用了广播接收器。 在扫描活动屏幕上,有一些条形码需要扫描。我设置了传输信息的意图,从中执行了edittext扫描(使用putextra)。广播接收器接收扫描操作,但putextra的输入不存在(handle变量获得空值[附件]) 我很乐意帮助解决我做错的事情 活动类: public class MoveItemActivit

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

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

活动类

                        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;
           }
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);
        }
    }
}
广播接收机类

                        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;
           }
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\u BARCODE\u OPEN
ACTION,所以我想它没有收到。

Hi micer。请注意,操作\u条形码\u回调\u解码\u数据是注册的意图过滤操作,而操作\u条形码\u打开是意图操作。还有其他选择吗?