question

ramya avatar image
ramya asked ramya commented

Barcode scanner not working in service

Hi,

I want to use Barcode scanner in Service, but scanner not receiving code. Please check this.

public class BarcodeService extends Service {
    private BarcodeScanner mBarcodeScanner;
    private BroadcastReceiver barcodeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            BarcodeResult barcodeResult = new BarcodeResult(intent);

            if (barcodeResult.isBarcodeAction()) {
                String barcode = barcodeResult.getBarcode();

            }
        }
    };
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        mBarcodeScanner = new BarcodeScanner(this);
        registerBarcodeScanner();
        Bundle extras = new Bundle();
        extras.putBoolean(Intents.EXTRA_LED_ON, true);
        extras.putBoolean(Intents.EXTRA_SCAN_QR_CODE, false);
        extras.putBoolean(Intents.EXTRA_SCAN_1D_CODE, true);
        extras.putBoolean(Intents.EXTRA_SHOW_PREVIEW, false);
        extras.putInt(Intents.EXTRA_SCANNER_FACING, BarcodeScanner.BARCODE_SCANNER_FACING_CUSTOMER);
        mBarcodeScanner.executeStartScan(extras);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterBarcodeScanner();
    }
    private void registerBarcodeScanner() {
        registerReceiver(barcodeReceiver, new IntentFilter(BarcodeResult.INTENT_ACTION));
    }
    private void unregisterBarcodeScanner() {

        unregisterReceiver(barcodeReceiver);
    }
}


Thank you.

barcode scanner
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
David Marginian avatar image
David Marginian Deactivated answered ramya commented
3 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Yes, Its working fine in Activity but not working in Service.
0 Likes 0 ·
It isn't clear to me why you need this in a service. You seem to be making some assumptions above services in Android that aren't correct. You may want to perform some searches on the internet for using Android services in general (when to use them, limitations, etc.).
0 Likes 0 ·
ramya avatar image ramya David Marginian ♦♦ ·
We have to perform scanning operation in Register app that is the reason we are using service.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community