【问题标题】:Starting my app after QR code has been scanned扫描二维码后启动我的应用程序
【发布时间】:2020-06-18 23:17:39
【问题描述】:

我实现了一个读取 QR 码并以模式显示结果的应用程序(然后,如果结果是 URL,则在 WebView 上重定向)。它运作良好。 但现在我想做的是:当使用我设备的相机(不是我的应用程序)扫描二维码时,它会启动我的应用程序并在我的模式中显示结果。

我使用了意图过滤器来做到这一点......而且效果也很好!但问题是它不仅会在扫描二维码的情况下启动我的应用程序。例如,当我单击手机上的 URL 时,它会启动我的应用程序。这不是我想要的行为。我希望它仅在从 QR 码中检测到 URL 时启动我的应用程序。有可能吗?

这是我的清单

 <activity
            android:launchMode="singleTask"
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:scheme="http" />
                <data android:scheme="https" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>


                <data android:scheme="http" android:host="*"/>
                <data android:scheme="https" android:host="*"/>
            </intent-filter>

            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>

【问题讨论】:

    标签: android android-intent android-manifest


    【解决方案1】:

    为了只为二维码打开您的应用,您需要为您的应用指定自定义数据架构,而不是 http 和 https:

    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
    
        <data android:scheme="myCustomSchemeName"/>
    </intent-filter>
    

    然后带有myCustomSchemeName://openSomething?param=42 的二维码将在您的应用中打开。

    【讨论】:

    • 当然,但我想要打开每个 URL。感谢您的帮助
    猜你喜欢
    • 2022-07-22
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 2016-09-17
    相关资源
    最近更新 更多