Android NFC Intent尚未启动我的活动 [英] Android NFC Intents are not starting my activity

查看:387
本文介绍了Android NFC Intent尚未启动我的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个与NFC标签进行交互的简单应用程序,但是我似乎无法让手机执行任何操作,只能触发默认的NFC标签应用程序.我真的只是想能够拦截我扫描的任何标签,确定标签上是否有一些数据,然后采取相应的措施.

I am trying to write a simple application to interact with NFC tags, but I cant seem to get my phone to do anything but trigger the default NFC tag app. I really just want to be able to intercept any tag I scan, determine if it has some data on it, and take action accordingly.

现在我的清单文件看起来像

Right now my manifest file looks like

<uses-sdk android:minSdkVersion="10" />
<uses-feature android:name="android.hardware.nfc" android:required="true"/>
<uses-permission android:name="android.permission.NFC"/>

<application 
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".NfcActivity"
        android:label="@string/app_name">
        <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"/>
        </intent-filter>
    </activity>
</application>

但是,当扫描NFC标签时,我从未看到活动开始.我在这里想念什么吗?我试图将意图过滤器放在BroadcastReceiver内,也没有运气...

However when scanning an NFC tag, I never see the activity start. Am I missing something here? I tried placing the intent filter inside a BroadcastReceiver and had no luck either...

推荐答案

您无法通过扫描的所有NFC标签来启动您的应用. Android将根据意图过滤器的具体程度来确定最合适的应用程序.但是,如果您的应用程序在前台运行,则可以使用 NFC前台调度以捕获所有NFC意图.

You cannot have your app started by all NFC tags you scan. Android will determine what the most suitable app is based on how specific the intent filter is. However, if your app is running in the foreground, you can use NFC foreground dispatch to catch all NFC intents.

onCreate()中添加:

mAdapter = NfcAdapter.getDefaultAdapter(this);
PendingIntent pendingIntent = PendingIntent.getActivity(
  this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

onResume()中添加:

mAdapter.enableForegroundDispatch(this, pendingIntent, null, null);

onPause()中添加:

mAdapter.disableForegroundDispatch(this);

onNewIntent中,您可以像这样获得NFC标签:

In onNewIntent you can get at the NFC tag like this:

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

这篇关于Android NFC Intent尚未启动我的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆