接收广播时阻止应用程序切换

时间:2016-08-15 20:00:51

标签: android broadcastreceiver

我正在使用BroadcastReceiver从隐藏的应用程序中侦听服务。当我从隐藏的应用程序收到广播消息时,它会转到第一个计划,使接收器应用程序转到后台。我该如何防止这种行为?

编辑1

   private void ExecBarcode()
    {
        final TextView barcodeResult = (TextView)findViewById(R.id.txvBarcodeResult);

        BroadcastReceiver actionBarcodeScannerAndPresenceReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
            /*  Receiver for the barcode scan and presence sensor events    */
                if (intent.getAction().equals(Constants.ACTION_PRESENCE)) {
                    try
                    {
                        Log.e("Sensor Presenca","Detectada");
                    }
                    catch (Exception e) {
                        Toast.makeText(getApplicationContext(), "Ocorreu um erro no sensor de presença", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                } else if (intent.getAction().equals(Constants.ACTION_ON_BARCODE)) {
                    try {
                        String scannedText = intent.getStringExtra(Constants.BARCODE_TEXT);
                        barcodeResult.setText(scannedText);
                    }
                    catch (Exception e){
                        Toast.makeText(getApplicationContext(), "Ocorreu um erro ao ler o código de barras!", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }

            }
        };

        IntentFilter filter = new IntentFilter();
        filter.addAction(Constants.ACTION_ON_BARCODE);
        filter.addAction(Constants.ACTION_PRESENCE);
        registerReceiver(actionBarcodeScannerAndPresenceReceiver, filter);
    }

上面的代码用于接收广播事件。传输消息时,广播消息的应用程序在后台运行。

0 个答案:

没有答案