我正在创建一个简单的Android移动应用程序,用于从NFC标签读取数据。我希望我的应用程序在检测到NFC标签被点击后打开。我无法弄清楚我的代码可能会遗漏什么。当我尝试点按NFC标签时,它会继续打开Google Play商店。
AnroidManifest.xml
<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:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ItemDetailsActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".NFCActivity">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
NFCActivity
public class NFCActivity extends AppCompatActivity {
private static final String TAG = "[NFCActivity]";
public static final String MIME_TEXT_PLAIN = "text/plain";
private NfcAdapter mNfcAdapter;
private Context context;
private PendingIntent pendingIntent;
private IntentFilter[] readTagFilters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate");
setContentView(R.layout.activity_item_details);
context = getApplicationContext();
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(this,
0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP),
0);
IntentFilter ndefDetected = new IntentFilter((NfcAdapter.ACTION_NDEF_DISCOVERED));
IntentFilter discovery = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
readTagFilters = new IntentFilter[]{discovery};
}
@Override
protected void onResume() {
super.onResume();
Log.v(TAG, "onResume");
if(checkIfNfcEnableAndCapable()) {
Log.v(TAG, "Phone is NFC capable and NFC is enabled.");
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, readTagFilters, null);
}
}
@Override
protected void onPause() {
Log.v(TAG, "onPause");
if(checkIfNfcEnableAndCapable()) {
mNfcAdapter.disableForegroundDispatch(this);
}
super.onPause();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
Log.v(TAG, "handleIntent");
String action = intent.getAction();
Log.v(TAG, "action: " + action);
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
String type = intent.getType();
if(MIME_TEXT_PLAIN.equals(type)) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
new NdefReaderTask().execute(tag);
} else {
Log.v(TAG, "Wrong mime type: " + type);
}
} else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String[] techList = tag.getTechList();
String searchedTech = Ndef.class.getName();
for (String tech : techList) {
if (searchedTech.equals(tech)) {
new NdefReaderTask().execute(tag);
break;
}
}
} else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Log.v(TAG, tag.toString());
Log.v(TAG, tag.getTechList().toString());
}
}
private boolean checkIfNfcEnableAndCapable() {
if(mNfcAdapter == null) {
Log.v(TAG, "This device doesn't support NFC");
return false;
}
if(!mNfcAdapter.isEnabled()) {
Log.v(TAG, "NFC is disabled.");
return false;
}
return true;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "[MainActivity]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v(TAG, "onCreate");
GridView gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new ViewAdapter(this, MainActivity.this));
}}
ItemDetailsActivity
public class ItemDetailsActivity extends Activity {
private static final String TAG = "[ItemDetailsActivity]";
private ImageLoader imageLoader = ImageLoader.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_details);
imageLoader.init(ImageLoaderConfiguration.createDefault(this));
Intent intent = getIntent();
String action = intent.getAction();
Log.v(TAG, "Intent: " + action);
EventItems item = (EventItems) intent.getSerializableExtra("item");
if (item == null) {
Log.v(TAG, "item is null");
} else {
Log.v(TAG, "Item ID: " + item.getId());
SquaredImageView imageView = (SquaredImageView) findViewById(R.id.details_picture);
imageLoader.displayImage(item.getImageUrl(), imageView);
}
}}
请让我知道我错过了什么。 谢谢!