首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >斑马数据楔外的主要活动。如何配置意图筛选器?

斑马数据楔外的主要活动。如何配置意图筛选器?
EN

Stack Overflow用户
提问于 2022-05-04 13:51:51
回答 1查看 393关注 0票数 1

新的Android和Kotlin,我需要帮助意图和意图过滤器。我使用的是斑马MC2700和AndroidStudio2021& Kotlin。我的主要活动设置了DataWedge配置文件,然后启动另一个活动。第二个活动应该有一个意图过滤器,这样我就可以使用onNewIntent了。这个过程在本教程中得到了很好的演示,https://github.com/darryncampbell/DataWedge-GettingStarted-Samples我能够复制和修改该应用程序。但是我不能让我的OnIntent例程在除了主活动之外的任何东西中被调用。

我还读过“在斑马条形码扫描仪上使用DataWedge进行多个活动不适用于Kotlin”的主题,但我仍然缺少一些东西。当然,这与Android清单和意图过滤器/侦听器设置有关。

除筛选操作外,DWUtilities.kt文件与示例相同:

代码语言:javascript
复制
        intentProps.putString(
            "intent_action",
              "com.example.simplescan.ACTION")

我的主要活动有一个按钮来启动第二个活动。

代码语言:javascript
复制
                    val intent = Intent(this, SubActivityConsume::class.java)
                    startActivity(intent)

这是第二个应该处理扫描的活动:

代码语言:javascript
复制
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文件,以防我遗漏了其他问题)。

代码语言:javascript
复制
<?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()从未被调用过。

EN

回答 1

Stack Overflow用户

发布于 2022-07-20 20:48:03

我对此也有问题;我让它工作的方式是将概要文件设置为广播意图,并在需要它的活动中设置一个BroadcastReceiver。

在DWUtilities.kt中,更改

代码语言:javascript
复制
intentProps.putString("intent_delivery", "0") //  StartActivity

代码语言:javascript
复制
intentProps.putString("intent_delivery", "2") //  Broadcast Intent

然后在SubActivityConsume.kt设置你的广播接收器..。

代码语言:javascript
复制
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")
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72114153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档