Zebra Datawedge outside of Main Activity. How to configure the Intent Filter?

870 views Asked by At

New to Android & Kotlin, and I need help with Intents and Intent Filters. I'm using a Zebra MC2700 with Android Studio 2021 & Kotlin. My Main Activity sets up the DataWedge profile and then starts another activity. The second activity should have an intent filter, so that I can make use of onNewIntent. The process is nicely demonstrated in this tutorial https://github.com/darryncampbell/DataWedge-GettingStarted-Samples I was able to duplicate & modify that app. But I cannot get my OnIntent routine to be called in anything other than the main activity.

I have also read the topic "Using DataWedge for Multiple Activities on Zebra Barcode Scanner doesn't work in Kotlin" But I'm still missing something. Surely it has to do with the Android manifest and the Intent Filter / Listener setup.

The DWUtilities.kt file is the same as the example except for the filter action:

        intentProps.putString(
            "intent_action",
              "com.example.simplescan.ACTION")

My Main Activity has a button that launches the second activity.

                    val intent = Intent(this, SubActivityConsume::class.java)
                    startActivity(intent)

This is the second activity where the scan should be handled:

class SubActivityConsume : AppCompatActivity(), View.OnTouchListener{

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sub_consume)
        val btnScan = findViewById<Button>(R.id.btnScan)
        btnScan.setOnTouchListener(this)
    }

// Zebra DataWedge Stuff
override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    displayScanResult(intent)
}

and here is my latest Android Manifest (Edited to have the whole .xml file in case there are other issues I'm missing)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simplescan">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SimpleScan">
        <activity
            android:name=".ViewLists"
            android:exported="false"
            android:label="View Lists" />
        <activity
            android:name=".SubActivityConsume"
            android:exported="false"
            android:label="Scan Consumed Material"
            android:launchMode="singleTop">
        <intent-filter>
            <action android:name="com.example.simplescan.ACTION" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        </activity>
        <activity
            android:name=".SubActivityReceive"
            android:exported="false"
            android:label="Scan Received Material" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

Both the soft trigger and the device trigger fire the scanner, and it reads the barcode, and a beep is heard. But, onNewIntent() is never called.

1

There are 1 answers

0
aPaul On

I also had problems with this; the way I got it working was to set the profile to Broadcast Intent, and set a BroadcastReceiver in the activity that needs it.

In DWUtilities.kt, change

intentProps.putString("intent_delivery", "0") //  StartActivity

to

intentProps.putString("intent_delivery", "2") //  Broadcast Intent

Then in SubActivityConsume.kt set up your broadcast receiver...

class SubActivityConsume : AppCompatActivity(), View.OnTouchListener{
        lateinit var broadcastReceiver: BroadcastRecevier
            
                override fun onCreate(savedInstanceState: Bundle?) {
                    super.onCreate(savedInstanceState)
                    setContentView(R.layout.activity_sub_consume)
                    val btnScan = findViewById<Button>(R.id.btnScan)
                    btnScan.setOnTouchListener(this)
    
    
                   broadcastReceiver = object : BroadcastReceiver() {
                    override fun onReceive(context: Context, scanIntent: Intent?) {
                        Log.v("SCAN","broadcast received: ${scanIntent?.action}")
                        when (scanIntent?.action) {
                            "com.example.simplescan.ACTION" -> {
                                 displayScanResult(scanIntent)
                            }
                        }
                    }
                }
                DWUtilities.CreateDWProfile(this)
        }

        override fun onResume() {
                super.onResume()
                this.registerReceiver(broadcastReceiver, 
     IntentFilter("com.example.simplescan.ACTION"))
                Log.v("SCAN","Broadcast receiver registered")
            }
        
            override fun onPause() {
                super.onPause()
                this.unregisterReceiver(broadcastReceiver)
                Log.v("SCAN","Broadcast receiver unregistered")
            }