我最近创建了一个 NFC 标签读取器应用程序,但不幸的是,我的应用程序似乎无法识别它,但它被系统(移动)软件检测到并重定向到内置 NFC 扫描仪。我已经仔细检查了我的代码和连接,并且按照官方文档看起来似乎已正确,但我仍然面临这个问题。
问题:应用程序未检测到
处的NFC回调,它重定向到内置功能。与其他(下载的)应用程序配合良好。onNewIntent()
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.NFCApp"
tools:targetApi="31">
<activity
android:name=".AnotherActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop">
<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" />
<data android:mimeType="text/plain" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
</application>
</manifest>
nfc_tech_filter.xml位于
<project-root>/res/xml
:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.NfcB</tech>
</tech-list>
</resources>
主要活动:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var nfcAdapter: NfcAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
nfcAdapter = NfcAdapter.getDefaultAdapter(this)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Log.d("CHKI", "NEW NFC DETECTED")
if (intent?.action == NfcAdapter.ACTION_NDEF_DISCOVERED) {
Log.d("CHKI", "ACTION_NDEF_DISCOVERED")
}
if (intent?.action == NfcAdapter.ACTION_TECH_DISCOVERED) {
Log.d("CHKI", "ACTION_TECH_DISCOVERED")
}
}
}
其他人是否遇到过类似的问题?如果有,您采取了哪些步骤来解决?任何见解或建议将不胜感激!预先感谢。
刚刚添加了
onResume
和 onPause
,现在 NFC 可以与我现有的代码正常工作。这些方法用于启用和禁用前台调度系统。
这是代码:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var nfcAdapter: NfcAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// existing code
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
// existing code
}
// Added new
override fun onResume() {
super.onResume()
val pendingIntent = PendingIntent.getActivity(
this,
0,
Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
PendingIntent.FLAG_MUTABLE
)
nfcAdapter?.enableForegroundDispatch(this, pendingIntent, null, null)
}
// Added new
override fun onPause() {
super.onPause()
nfcAdapter?.disableForegroundDispatch(this)
}
}