我想在其他几个NFC应用程序中创建一个特定的NFC应用程序,它应该具有以下功能
在这里,我已经实现了前台调度系统,但是它只列出了所有具有NDEF_DISCOVERED优先级的NFC应用程序。
清单文件的代码片段如下:
<activity android:name=".NFCDiscovered" android:label="@string/app_name">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
</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" />在这里,NFCDiscovered.java负责读取具有前景分派系统的特定文本类型NFC标记。用于此的代码片段如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nfcreader);
mAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0,new
Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("text/plain");
} catch (MalformedMimeTypeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mFilters = new IntentFilter[] { ndef };
//resolveIntent(getIntent());
}
public void onResume() {
super.onResume();
// TODO Auto-generated method stub
TextView myText= (TextView)findViewById(R.id.nfcTxt);
super.onResume();
if (mAdapter != null)
mAdapter.enableForegroundDispatch(this, mPendingIntent,
mFilters,null);
}
protected void onNewIntent(Intent intent) {
NdefMessage[] msgs = null;
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
}
List<TextRecord> records = NdefMessageParser.parse(msgs[0]);
String text = null;
for(TextRecord local:records)
{
text = local.getText();
}
//myText.setText("text :"+msgs);
}
public void onPause() {
super.onPause();
if (mAdapter != null) mAdapter.disableForegroundDispatch(this);
}如果有人能告诉我我错过了什么就太好了。
提前感谢
发布于 2012-02-26 21:59:50
如果您控制了NFC标记的内容,并且正在使用Android4.0,请考虑在文本消息之后添加一个Android应用程序记录。
发布于 2015-04-24 20:17:44
三项评论:
然后,您可以专注于NFC消息本身,而不是活动分派。
https://stackoverflow.com/questions/9126335
复制相似问题