Android-NFC中的前台调度系统问题
在其他几个NFC应用程序中,我想制作一个特定的NFC应用程序,它应该具有以下功能Android-NFC中的前台调度系统问题,android,nfc,Android,Nfc,在其他几个NFC应用程序中,我想制作一个特定的NFC应用程序,它应该具有以下功能 1.只有在设备上扫描具有特定文本(NDEF类型)的NFC标签时,才应启动应用程序。(这意味着如果点击任何其他文本类型的标记,则不应显示我的应用程序) 启动应用程序时,“使用完整操作”选项和设备中所有NFC应用程序的列表不应出现 在这里,我实现了前台调度系统,但它只列出了具有NDEFèU发现优先级的所有NFC应用程序 清单文件的代码段如下所示: <activity android:name=".NFCDi
1.只有在设备上扫描具有特定文本(NDEF类型)的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);
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.nfcreader);
mAdapter=NfcAdapter.getDefaultAdapter(此);
mpendingcontent=pendingent.getActivity(此,0,新
Intent(this,getClass()).addFlags(Intent.FLAG\u ACTIVITY\u SINGLE\u TOP),0;
IntentFilter ndef=新的IntentFilter(NfcAdapter.ACTION\u ndef\u发现);
试一试{
ndef.addDataType(“文本/普通”);
}捕获(格式错误的dmimetype异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
mFilters=newintentfilter[]{ndef};
//resolveIntent(getIntent());
}
恢复时公开作废(){
super.onResume();
//TODO自动生成的方法存根
TextView myText=(TextView)findViewById(R.id.nfcTxt);
super.onResume();
如果(mAdapter!=null)
mAdapter.enableForegroundDispatch(本,MPendingContent,
mfilter,空);
}
受保护的void onNewIntent(意图){
NdefMessage[]msgs=null;
if(NfcAdapter.ACTION\u NDEF\u DISCOVERED.equals(getIntent().getAction())){
Parcelable[]rawMsgs=intent.getParcelableArrayExtra(NfcAdapter.EXTRA\u NDEF\u消息);
if(rawMsgs!=null){
msgs=新的NdefMessage[rawMsgs.length];
对于(int i=0;i
如果有人能告诉我我错过了什么,那就太好了。提前感谢 < P>如果您对NFC标签的内容有控制权,并且使用安卓4.0,请考虑在文本消息之后追加一个。 < P三评论:
@Override
public void onResume(){
super.onResume();
// here to scan code.
NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
// disable foreground dispatch
mNfcAdapter.disableForegroundDispatch(this);
Bundle bundle = new Bundle();
//
mNfcAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A, null) };
然后,您可以关注NFC消息本身,而不是活动调度。我不清楚您是否正在使用已启动的应用程序在前台扫描标记,因为您说“应用程序应仅在具有特定文本的NFC标记时启动…”前台调度仅在应用程序位于前台时有效。如果它在前景中,那么您过滤意图的方式或意图实际启动的方式都有问题。当您扫描标签并将其粘贴到此处时,是否可以检查logcat以查看启动的意图?