| | |
| | | import android.nfc.NfcEvent; |
| | | import android.nfc.Tag; |
| | | import android.nfc.cardemulation.CardEmulation; |
| | | import android.os.Bundle; |
| | | import android.os.Handler; |
| | | import android.os.Message; |
| | | |
| | | import com.dayu.recharge.card.UserCard; |
| | | import com.dayu.recharge.tools.LoyaltyCardReader; |
| | | import com.dayu.recharge.tools.NFCUtil; |
| | | import com.dayu.recharge.tools.NfcReadHelper; |
| | | import com.dayu.recharge.utils.LogUtil; |
| | | import com.dayu.recharge.utils.TipUtil; |
| | |
| | | |
| | | public final static int ERROR = -1; |
| | | |
| | | protected NfcAdapter mNfcAdapter; |
| | | private PendingIntent mPendingIntent; |
| | | // protected NfcAdapter mNfcAdapter; |
| | | // private PendingIntent mPendingIntent; |
| | | |
| | | volatile UserCard userCard; |
| | | Handler handler; |
| | | |
| | | public LoyaltyCardReader mLoyaltyCardReader; |
| | | |
| | | NFCUtil nfcUtil; |
| | | |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | nfcUtil = NFCUtil.getInstance(); |
| | | nfcUtil.setAccountCallback(this); |
| | | } |
| | | |
| | | /** |
| | | * onCreat->onStart->onResume->onPause->onStop->onDestroy |
| | | * 启动Activity,界面可见时. |
| | | */ |
| | | |
| | | public static int READER_FLAGS = |
| | | NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK; |
| | | |
| | | @Override |
| | | protected void onStart() { |
| | | super.onStart(); |
| | | //此处adapter需要重新获取,否则无法获取message |
| | | mNfcAdapter = NfcAdapter.getDefaultAdapter(this); |
| | | Intent intent = new Intent(this, getClass()); |
| | | |
| | | //一旦截获NFC消息,就会通过PendingIntent调用窗口 |
| | | mPendingIntent = PendingIntent.getActivity(this, 0, intent, 0); |
| | | |
| | | // mLoyaltyCardReader = new LoyaltyCardReader(this); |
| | | // if (mNfcAdapter != null) { |
| | | // mNfcAdapter.enableReaderMode(this, mLoyaltyCardReader, READER_FLAGS, null); |
| | | // } |
| | | nfcUtil.onStartNfcAdapter(this); |
| | | |
| | | } |
| | | |
| | |
| | | @Override |
| | | public void onResume() { |
| | | super.onResume(); |
| | | if (!ifNFCUse(this)) { |
| | | this.finish(); |
| | | } |
| | | //设置处理优于所有其他NFC的处理 |
| | | if (mNfcAdapter != null) |
| | | mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); |
| | | nfcUtil.onResumeNfcAdapter(this); |
| | | } |
| | | |
| | | /** |
| | |
| | | public void onPause() { |
| | | super.onPause(); |
| | | //恢复默认状态 |
| | | if (mNfcAdapter != null) |
| | | mNfcAdapter.disableForegroundDispatch(this); |
| | | // mNfcAdapter.disableReaderMode(this); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | | super.onDestroy(); |
| | | if (mNfcAdapter != null) |
| | | mNfcAdapter.disableReaderMode(this); |
| | | } |
| | | |
| | | /** |
| | | * 检测工作,判断设备的NFC支持情况 |
| | | * |
| | | * @return |
| | | */ |
| | | protected Boolean ifNFCUse(Activity context) { |
| | | if (mNfcAdapter == null) { |
| | | TipUtil.show(context, "设备不支持NFC!"); |
| | | return false; |
| | | } |
| | | if (mNfcAdapter != null && !mNfcAdapter.isEnabled()) { |
| | | TipUtil.show(context, "请在系统设置中先启用NFC功能!"); |
| | | return false; |
| | | } |
| | | return true; |
| | | nfcUtil.onPauseNfcAdapter(this); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void onAccountReceived(Tag tag) { |
| | | public void physicalCardDoing(Tag tag) { |
| | | |
| | | Intent intent = new Intent(this, getClass()); |
| | | intent.putExtra(NfcAdapter.EXTRA_TAG, tag); |
| | | startActivity(intent); |
| | | } |
| | | |
| | | @Override |
| | | public void virtualCardDoing(Tag tag) { |
| | | |
| | | } |
| | | } |