|
package com.example.tpsmodule.nfc;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.nfc.tech.MifareClassic;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;
import com.example.tpsmodule.R;
import com.example.tpsmodule.StringUtils;
import java.util.Timer;
import java.util.TimerTask;
public class NfcDialog extends Activity {
//---------控件定义------------
// NFC parts
private static NfcAdapter nfcAdapter;
private static PendingIntent mPendingIntent;
private static IntentFilter[] mFilters;
private static String[][] mTechLists;
private Intent mIntent = null;
private String TAG="NfcDialog";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_nfc);
Log.e(TAG,"onCreate");
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
NfcTag.getInstance().isflg=false;
// Toast.makeText(getApplicationContext(), "No support NFC!", Toast.LENGTH_SHORT).show();
return;
}
if (!nfcAdapter.isEnabled()) {
NfcTag.getInstance().isflg=false;
Toast.makeText(getApplicationContext(), "请在系统设置中先启用NFC功能!", Toast.LENGTH_SHORT).show();
}
NfcTag.getInstance().isflg=true;
// Toast.makeText(getApplicationContext(), "NFC打开成功", Toast.LENGTH_SHORT).show();
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mFilters = new IntentFilter[]{ndef};
//设置
mTechLists = new String[][]{new String[]{IsoDep.class.getName()}, new String[]{MifareClassic.class.getName()}};//MifareClassic
mIntent = this.getIntent();
//启动线程关闭页面
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(!NfcTag.getInstance().getisflg())
{
finish();
}
}
},1000);
}
@Override
public void onResume() {
super.onResume();
//-----------------非常关键,必要的哦,不能删除----------------
nfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
mTechLists);
}
@Override
protected void onStart() {
super.onStart();
if(!NfcTag.getInstance().getisflg())
{
finish();
}
}
protected void onPause () {
super.onPause();
finish();
}
@Override
public void onNewIntent(Intent intent) {
if(!NfcTag.getInstance().getisflg())
{
finish();
}else {
mIntent = intent;
String action = intent.getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {//当前的action
Tag tag = mIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] bytCardSN = tag.getId();
String strCardSN = StringUtils.bytesToHexString(bytCardSN);
NfcTag.getInstance().setId(strCardSN);
finish();//读取完成关闭页面
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG,"onDestroy");
}
/**
* 注意:
* super.onBackPressed()会自动调用finish()方法,关闭当前Activity.
*/
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
return false;
}else {
return super.onKeyDown(keyCode, event);
}
}
}
就是找不到问题啊 |
|