扫描NFC标签时无意

时间:2019-09-06 10:39:35

标签: android kotlin 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()
    }
}

AndroidManifest.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>

我认为应该显示一个带有意图名称的Toast,但是什么也没有出现。

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我不太确定,但是请尝试以下操作:

<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()
    }
}

它必须工作。