新的Android和Kotlin,我需要帮助意图和意图过滤器。我使用的是斑马MC2700和AndroidStudio2021& Kotlin。我的主要活动设置了DataWedge配置文件,然后启动另一个活动。第二个活动应该有一个意图过滤器,这样我就可以使用onNewIntent了。这个过程在本教程中得到了很好的演示,https://github.com/darryncampbell/DataWedge-GettingStarted-Samples我能够复制和修改该应用程序。但是我不能让我的OnIntent例程在除了主活动之外的任何东西中被调用。
我还读过“在斑马条形码扫描仪上使用DataWedge进行多个活动不适用于Kotlin”的主题,但我仍然缺少一些东西。当然,这与Android清单和意图过滤器/侦听器设置有关。
除筛选操作外,DWUtilities.kt文件与示例相同:
intentProps.putString(
"intent_action",
"com.example.simplescan.ACTION")我的主要活动有一个按钮来启动第二个活动。
val intent = Intent(this, SubActivityConsume::class.java)
startActivity(intent)这是第二个应该处理扫描的活动:
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)
}这是我最新的(编辑好了整个.xml文件,以防我遗漏了其他问题)。
<?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>软触发器和设备触发器都会触发扫描仪,然后读取条形码,然后听到嘟嘟声。但是,onNewIntent()从未被调用过。
发布于 2022-07-20 20:48:03
我对此也有问题;我让它工作的方式是将概要文件设置为广播意图,并在需要它的活动中设置一个BroadcastReceiver。
在DWUtilities.kt中,更改
intentProps.putString("intent_delivery", "0") // StartActivity至
intentProps.putString("intent_delivery", "2") // Broadcast Intent然后在SubActivityConsume.kt设置你的广播接收器..。
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")
}https://stackoverflow.com/questions/72114153
复制相似问题