Android:当扫描NFC标签时,如何禁用应用程序重新打开?
我正在编写一个android应用程序,从Mifare经典卡(4k)读取数据。我已经编辑了我的AndroidManifest.xml文件,以便启动应用程序(或者我可以选择另一个使用NFC的应用程序)。但当我的应用程序打开时,我将卡放在手机旁边,它会再次弹出窗口,我可以选择打开哪个应用程序。经过一些研究,我发现我需要编辑函数:Android:当扫描NFC标签时,如何禁用应用程序重新打开?,android,android-activity,nfc,intentfilter,mifare,Android,Android Activity,Nfc,Intentfilter,Mifare,我正在编写一个android应用程序,从Mifare经典卡(4k)读取数据。我已经编辑了我的AndroidManifest.xml文件,以便启动应用程序(或者我可以选择另一个使用NFC的应用程序)。但当我的应用程序打开时,我将卡放在手机旁边,它会再次弹出窗口,我可以选择打开哪个应用程序。经过一些研究,我发现我需要编辑函数:onNewIntent,因为这是在应用程序运行时扫描标记时调用的函数 这是我的代码:(当我扫描我的卡片时,handleIntent函数第二行的吐司内容是:ACTION\u TE
onNewIntent
,因为这是在应用程序运行时扫描标记时调用的函数
这是我的代码:(当我扫描我的卡片时,handleIntent
函数第二行的吐司内容是:ACTION\u TECH\u DISCOVERED
)
包be.khleuven.aanwezighidessysteem;
导入android.app.Activity;
导入android.app.pendingent;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.nfc.NdefMessage;
导入android.nfc.NdefRecord;
导入android.nfc.NfcAdapter;
导入android.nfc.Tag;
导入android.nfc.tech.Ndef;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.content.IntentFilter.MalformedMimeTypeException;
导入java.io.UnsupportedEncodingException;
导入java.util.array;
公共类登录扩展活动{
公共静态最终字符串MIME\u TEXT\u PLAIN=“TEXT/PLAIN”;
公共静态最终字符串TAG=“NfcDemo”;
私有文本视图mTextView;
专用NfcAdapter mNfcAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
mTextView=(TextView)findViewById(R.id.TextView\u解释);
mNfcAdapter=NfcAdapter.getDefaultAdapter(此);
if(mNfcAdapter==null){
//到此为止,我们绝对需要NFC
Toast.makeText(此“此设备不支持NFC.”,Toast.LENGTH_LONG.show();
//完成();
返回;
}
如果(!mNfcAdapter.isEnabled()){
setText(“NFC被禁用”);
}否则{
mTextView.setText(R.string.explainion);
}
handleIntent(getIntent());
}
@凌驾
受保护的void onResume(){
super.onResume();
/**
*重要的是,活动位于前台(已恢复)。否则
*将引发一个非法状态异常。
*/
setupForegroundDispatch(此,mNfcAdapter);
}
@凌驾
受保护的void onPause(){
/**
*在onPause之前调用此函数,否则也会引发IllegalArgumentException。
*/
stopForegroundDispatch(此,mNfcAdapter);
super.onPause();
}
@凌驾
公共帐篷(意向){
/**
*当新的意图与当前活动实例关联时,将调用此方法。
*将调用onNewIntent而不是创建新活动。有关更多信息,请查看
*在文档中。
*
*在我们的例子中,当用户将标签附加到设备时,会调用此方法。
*/
手册内容(意图);
}
私人无效手册内容(意图){
String action=intent.getAction();
Toast.makeText(this,action,Toast.LENGTH_LONG).show();
if(NfcAdapter.ACTION\u NDEF\u DISCOVERED.equals(ACTION)){
字符串类型=intent.getType();
if(MIME_TEXT_PLAIN.equals(type)){
Tag Tag=intent.getParcelableExtra(NfcAdapter.EXTRA_Tag);
新建NdefReaderTask().execute(标记);
}否则{
Log.d(标记“错误的mime类型:”+type);
}
}else if(NfcAdapter.ACTION\u TECH\u DISCOVERED.equals(ACTION)){
Toast.makeText(这是“zezijn gelijk”,Toast.LENGTH_LONG.show();
//以防我们仍然使用这项技术
Tag Tag=intent.getParcelableExtra(NfcAdapter.EXTRA_Tag);
String[]techList=tag.getTechList();
字符串searchedTech=Ndef.class.getName();
用于(字符串技术:技术列表){
如果(搜索技术等于(技术)){
新建NdefReaderTask().execute(标记);
打破
}
}
TextView t=(TextView)findViewById(R.id.TextView\u解释);
t、 setText(“Mooi kaartje heb je daar”);
}
else if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(ACTION)){
//以防我们仍然使用这项技术
Tag Tag=intent.getParcelableExtra(NfcAdapter.EXTRA_Tag);
String[]techList=tag.getTechList();
字符串searchedTech=Ndef.class.getName();
用于(字符串技术:技术列表){
如果(搜索技术等于(技术)){
新建NdefReaderTask().execute(标记);
打破
}
}
}
if(NfcAdapter.ACTION\u TECH\u DISCOVERED.equals(intent.getAction())){
TextView TextView=(TextView)findViewById(R.id.TextView\u解释);
setText(“你好,NFC标签!”);
}否则{
//忽略
}
}
公共静态无效设置ForegroundDispatch(最终活动活动,NfcAdapter适配器){
最终意图=新意图(activity.getApplicationContext(),activity.getClass());
intent.setFlags(intent.FLAG\u ACTIVITY\u SINGLE\u TOP);
final pendingent pendingent=pendingent.getActivity(activity.getApplicationContext(),0,intent,0);
IntentFilter[]过滤器=新的IntentFilter[1];
字符串[][]技术列表=新字符串[][]{};
//请注意,这与清单中的筛选器相同。
过滤器[0]=新的IntentFilter();
筛选器[0].addAction(发现NfcAdapter.ACTION\u NDEF\u);
筛选器[0].addCategory(Intent.CATEGORY\u默认值);
试一试{
筛选器[0]。添加数据类型(MIME\u TEXT\u PLAIN);
}捕获(格式错误的dmimetype异常){
抛出新的RuntimeException(“检查mime类型”);
}
adapter.enableForegroundDispatch(活动、挂起内容、过滤器、技术列表);
}
公共静态void stopForegroundDispatch(最终活动活动,NfcAdapter适配器){
package be.khleuven.aanwezigheidssysteem;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.content.IntentFilter.MalformedMimeTypeException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class Login extends Activity {
public static final String MIME_TEXT_PLAIN = "text/plain";
public static final String TAG = "NfcDemo";
private TextView mTextView;
private NfcAdapter mNfcAdapter;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mTextView = (TextView) findViewById(R.id.textView_explanation);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
//finish();
return;
}
if (!mNfcAdapter.isEnabled()) {
mTextView.setText("NFC is disabled.");
} else {
mTextView.setText(R.string.explanation);
}
handleIntent(getIntent());
}
@Override
protected void onResume() {
super.onResume();
/**
* It's important, that the activity is in the foreground (resumed). Otherwise
* an IllegalStateException is thrown.
*/
setupForegroundDispatch(this, mNfcAdapter);
}
@Override
protected void onPause() {
/**
* Call this before onPause, otherwise an IllegalArgumentException is thrown as well.
*/
stopForegroundDispatch(this, mNfcAdapter);
super.onPause();
}
@Override
public void onNewIntent(Intent intent) {
/**
* This method gets called, when a new Intent gets associated with the current activity instance.
* Instead of creating a new activity, onNewIntent will be called. For more information have a look
* at the documentation.
*
* In our case this method gets called, when the user attaches a Tag to the device.
*/
handleIntent(intent);
}
private void handleIntent(Intent intent) {
String action = intent.getAction();
Toast.makeText(this, action, Toast.LENGTH_LONG).show();
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.d(TAG, "Wrong mime type: " + type);
}
} else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Toast.makeText(this, "Ze zijn gelijk", Toast.LENGTH_LONG).show();
// In case we would still use the Tech Discovered Intent
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;
}
}
TextView t = (TextView) findViewById(R.id.textView_explanation);
t.setText("Mooi kaartje heb je daar");
}
else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
// In case we would still use the Tech Discovered Intent
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;
}
}
}
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
TextView textView = (TextView) findViewById(R.id.textView_explanation);
textView.setText("Hello NFC tag!");
} else {
// ignore
}
}
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[][]{};
// Notice that this is the same filter as in our manifest.
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
try {
filters[0].addDataType(MIME_TEXT_PLAIN);
} catch (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);
}
private class NdefReaderTask extends AsyncTask<Tag, Void, String> {
@Override
protected String doInBackground(Tag... params) {
Tag tag = params[0];
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
// NDEF is not supported by this Tag.
return null;
}
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
try {
return readText(ndefRecord);
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unsupported Encoding", e);
}
}
}
return null;
}
}
}
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
filters[0].addDataType(MIME_TEXT_PLAIN);
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity, activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
adapter.enableForegroundDispatch(activity, pendingIntent, null, null);
}