我正在构建一个扫描NFC / Mifare卡并获取其唯一标识符的应用程序。不幸的是,尽管遵循了Android NFC api教程,但是在清单和前台调度程序中声明的意图都没有在设备中注册,因此Android选择打开默认的NFC应用程序。我的清单如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myname.application">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.NFC"/>
<uses-feature
android:name="android.hardware.nfc"
android:required ="true"/>
<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">
<activity android:name=".Scanner">
<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"/>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter"></meta-data>
</activity>
</application>
</manifest>
我的前台调度员如下:
@Override
protected void onResume() {
super.onResume();
setupForegroundDispatch(this, mNfcAdapter);
}
@Override
protected void onPause() {
stopForegroundDispatch(this, mNfcAdapter);
super.onPause();
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[1];
String[][] techList = new String[][]{};
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
try {
filters[0].addDataType(MIMETYPE_TEXT_PLAIN);
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("Check your mime type.");
}
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
adapter.disableForegroundDispatch(activity);
}
由于我不确定它是否有所作为,目前运行该应用程序的平台是华为P10 Plus。我过去曾遇到过一些兼容性问题,所以这可能是相关的。
答案 0 :(得分:1)
我成功完成了此操作,但未在清单中声明任何有关NFC的内容,仅声明了NFC权限。 我没有将过滤器和techList传递给frontendDispath,而是验证我收到的意图是nfc意图。
@Override
protected void onPause() {
super.onPause();
mAdapter = NfcAdapter.getDefaultAdapter(context);
if(mAdapter != null){
mAdapter.disableForegroundDispatch(activity);
}
}
@Override
protected void onResume() {
super.onResume();
mAdapter = NfcAdapter.getDefaultAdapter(context);
if(mAdapter != null) {
mPendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mAdapter.enableForegroundDispatch(activity, mPendingIntent, null, null);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if ( NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) ) {
readNFC(intent);
}
}