1
你好我每一个工作的android应用程序,我需要扫描设备的nfc标签。我完全新来nfc和阅读了很多教程后,我发现一些方法来检查nfc是否启用或不在手机中,但我不知道如何阅读nfc标签。如何阅读nfc标签与Android应用程序
这里是我的oncreat
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
mNfcAdapter = NfcAdapter.getDefaultAdapter(MainActivity.this);
pref = getApplicationContext().getSharedPreferences("Cordinate", 0);
editor = pref.edit();
nfc1 = (ImageView) findViewById(R.id.checknfc1);
mDisplay = (TextView) findViewById(R.id.display);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password1);
login = (TextView) findViewById(R.id.btnRegister);
context = getApplicationContext();
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
imei = telephonyManager.getDeviceId();
gcm = GoogleCloudMessaging.getInstance(this);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Toast.makeText(MainActivity.this, "i am workin",
// Toast.LENGTH_LONG).show();
GPSService gpstest = new GPSService(imei,MainActivity.this);
Thread data2 = new Thread(gpstest);
data2.start();
gps = new GPSTracker(MainActivity.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
//Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "GPS is not working", Toast.LENGTH_LONG).show();
}
username1 = username.getText().toString();
password2 = password.getText().toString();
username.setText("");
password.setText("");
new RegisterBackground().execute();
}
});
nfc1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mNfcAdapter != null) {
// "Read an NFC tag"
Toast.makeText(MainActivity.this, "Read an NFC tag", Toast.LENGTH_LONG).show();
// create an intent with tag data and deliver to this activity
mPendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
new Intent(MainActivity.this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// set an intent filter for all MIME data
IntentFilter ndefIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
onNewIntent(getIntent());
try {
ndefIntent.addDataType("*/*");
mIntentFilters = new IntentFilter[] { ndefIntent };
} catch (Exception e) {
Log.e("TagDispatch", e.toString());
}
} else {
Toast.makeText(MainActivity.this, "not able to read NFC tag", Toast.LENGTH_LONG).show();
}
//Toast.makeText(MainActivity.this, datanfc, Toast.LENGTH_LONG).show();
}
});
}
我onNewIntent
public void onNewIntent(Intent intent) {
String action = intent.getAction();
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String nfctag =tag.toString();
Toast.makeText(MainActivity.this, nfctag, Toast.LENGTH_LONG).show();
}
是什么意图的方法的作用得到了数据... – Simmant
它是用来打开你的应用程序时,您扫描NFC标签 – PsyGik
你的回答帮助我兄弟终于我得到解决方案 – Simmant