当我扫描 NFC 标签时没有意图



我正在尝试创建一个简单的NFC读卡器应用程序,但是当我尝试扫描卡时,我的应用程序似乎已经死了。

我尝试编辑 android 清单并添加nfc_tech_filter.xml,但我总是看到相同的结果。

我写过这些:

MainAvtivity.kt

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
print("ehi")
Toast.makeText(this, intent.action, Toast.LENGTH_SHORT).show()
}
}

安卓清单.xml

<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/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<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.TECH_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
</application>

nfc_tech_filter.xml

<?xml version="1.0" encoding="utf-8"?>
<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.MifareUltralight</tech>
<!-- class name -->
</tech-list>

我认为这应该出现一个带有意图名称的吐司,但什么也没有出现。

感谢您的帮助!

我不太确定,但试试这个:

<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/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
</application>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
}
override fun onNewIntent(intent: Intent) {
Toast.makeText(this, "ehi", Toast.LENGTH_SHORT).show()
super.onNewIntent(intent)
}
override fun onResume(){
// TODO: nfcAdapter enableForeground
}
override fun onPause(){
// TODO: nfcAdapter disableForeground()
}
}

它必须起作用。

最新更新