Decode Sample API

Settings Activity

The class com.datalogic.examples.decodesampleapi.SettingsActivity handles the application settings following Android guidelines as explained in Settings API Guide. The settings are defined in xml files in the folder res/xml/.

Scanner Service

The class com.datalogic.examples.decodesampleapi.ScannerService implements an Android  Service which is able to read barcodes when the application is not in foreground.

To use barcode listeners in a service, just register them as in any other Activity, the only difference is the entry point, which in the case of services is onStartCommand:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        BarcodeManager decoder = new BarcodeManager();
        decoder.addReadListener(this);
        decoder.addStartListener(this);
        decoder.addStopListener(this);
        decoder.addTimeoutListener(this);
        decoder.startDecode(5000);
        
    } catch (DecodeException e) {
        e.printStackTrace();
    }
    
    Log.d(MainActivity.TAG, "End service scan.");
    return Service.START_NOT_STICKY;
}

Android services must be declared in AndroidManifest.xml:

<service
    android:name="com.datalogic.examples.decodesampleapi.ScannerService"
    android:enabled="true" >
</service>