| | |
| | | */ |
| | | |
| | | |
| | | import android.app.Activity; |
| | | import android.app.PendingIntent; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.nfc.NdefMessage; |
| | | import android.nfc.NfcAdapter; |
| | | import android.nfc.NfcEvent; |
| | | import android.nfc.Tag; |
| | | import android.os.Handler; |
| | | import android.os.Message; |
| | | |
| | | import com.dayu.recharge.utils.ToastUtil; |
| | | import com.dayu.recharge.card.UserCard; |
| | | import com.dayu.recharge.tools.NfcReadHelper; |
| | | import com.dayu.recharge.utils.LogUtil; |
| | | import com.dayu.recharge.utils.TipUtil; |
| | | import com.tencent.bugly.crashreport.CrashReport; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * Author:Created by Ricky on 2017/8/25. |
| | |
| | | * 将NFC标签卡靠近手机后部(NFC标签卡可网上自行购买) |
| | | */ |
| | | public class BaseNfcActivity extends BaseActivity { |
| | | |
| | | /** |
| | | * 该卡已写入用户信息 |
| | | */ |
| | | public final static int HAS_USER = 1; |
| | | /** |
| | | * 该卡未写入用户信息 |
| | | */ |
| | | public final static int NO_USER = 2; |
| | | /** |
| | | * 充值 |
| | | */ |
| | | public final static int RECHARGE = 3; |
| | | |
| | | public final static int ERROR = -1; |
| | | |
| | | protected NfcAdapter mNfcAdapter; |
| | | private PendingIntent mPendingIntent; |
| | | |
| | | volatile UserCard userCard; |
| | | Handler handler; |
| | | |
| | | /** |
| | | * onCreat->onStart->onResume->onPause->onStop->onDestroy |
| | |
| | | super.onStart(); |
| | | //此处adapter需要重新获取,否则无法获取message |
| | | mNfcAdapter = NfcAdapter.getDefaultAdapter(this); |
| | | if (mNfcAdapter != null){ |
| | | mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() { |
| | | @Override |
| | | public NdefMessage createNdefMessage(NfcEvent nfcEvent) { |
| | | // 在此处处理NFC消息的创建 |
| | | return null; |
| | | } |
| | | }, this); |
| | | |
| | | } |
| | | // mNfcAdapter.enableReaderMode(this, new NfcAdapter.ReaderCallback() { |
| | | // @Override |
| | | // public void onTagDiscovered(Tag tag) { |
| | | // LogUtil.e(tag.toString()); |
| | | // } |
| | | // }, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK, null); |
| | | |
| | | //一旦截获NFC消息,就会通过PendingIntent调用窗口 |
| | | mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0); |
| | | |
| | |
| | | * |
| | | * @return |
| | | */ |
| | | protected Boolean ifNFCUse(Context context) { |
| | | protected Boolean ifNFCUse(Activity context) { |
| | | if (mNfcAdapter == null) { |
| | | ToastUtil.show(context, "设备不支持NFC!"); |
| | | TipUtil.show(context, "设备不支持NFC!"); |
| | | return false; |
| | | } |
| | | if (mNfcAdapter != null && !mNfcAdapter.isEnabled()) { |
| | | ToastUtil.show(context, "请在系统设置中先启用NFC功能!"); |
| | | TipUtil.show(context, "请在系统设置中先启用NFC功能!"); |
| | | return false; |
| | | } |
| | | return true; |