package com.dayu.henanlibrary.activity;
|
|
/**
|
* Created by zuo on 2018/12/2.
|
*/
|
|
|
import android.app.Activity;
|
import android.app.PendingIntent;
|
import android.content.Intent;
|
import android.nfc.NfcAdapter;
|
import android.os.Handler;
|
import android.provider.Settings;
|
|
import com.dayu.baselibrary.activity.BaseActivity;
|
import com.dayu.baselibrary.utils.TipUtil;
|
import com.dayu.baselibrary.utils.ToastUtil;
|
import com.dayu.henanlibrary.card.UserCard;
|
|
|
/**
|
* Author:Created by Ricky on 2017/8/25.
|
* Email:584182977@qq.com
|
* Description:
|
* 子类在onNewIntent方法中进行NFC标签相关操作。
|
* launchMode设置为singleTop或singelTask,保证Activity的重用唯一
|
* 在onNewIntent方法中执行intent传递过来的Tag数据
|
* 将NFC标签卡靠近手机后部(NFC标签卡可网上自行购买)
|
*/
|
public class BaseNfcActivity extends HNBaseActivity {
|
|
/**
|
* 该卡已写入用户信息
|
*/
|
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;
|
|
Handler handler;
|
|
volatile UserCard userCard;
|
/**
|
* 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);
|
// }
|
|
}
|
|
/**
|
* 获得焦点,按钮可以点击
|
*/
|
@Override
|
public void onResume() {
|
super.onResume();
|
if (!ifNFCUse(this)) {
|
|
}
|
//设置处理优于所有其他NFC的处理
|
if (mNfcAdapter != null)
|
mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
|
}
|
|
/**
|
* 暂停Activity,界面获取焦点,按钮可以点击
|
*/
|
@Override
|
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!", new TipUtil.TipListener() {
|
@Override
|
public void onCancle() {
|
BaseNfcActivity.this.finish();
|
}
|
});
|
return false;
|
}
|
if (mNfcAdapter != null && !mNfcAdapter.isEnabled()) {
|
startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
|
ToastUtil.showLong("请在系统设置中先启用NFC功能!");
|
return false;
|
}
|
return true;
|
}
|
|
}
|