左晓为主开发手持机充值管理机
zuoxiao
2023-12-04 1057af10a464e70d10b8a90315d0ecec6a7cb205
app/src/main/java/com/dayu/recharge/activity/BaseNfcActivity.java
@@ -13,10 +13,12 @@
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.nfc.Tag;
import android.nfc.cardemulation.CardEmulation;
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.NfcReadHelper;
import com.dayu.recharge.utils.LogUtil;
import com.dayu.recharge.utils.TipUtil;
@@ -34,7 +36,7 @@
 * 在onNewIntent方法中执行intent传递过来的Tag数据
 * 将NFC标签卡靠近手机后部(NFC标签卡可网上自行购买)
 */
public class BaseNfcActivity extends BaseActivity {
public class BaseNfcActivity extends BaseActivity implements LoyaltyCardReader.AccountCallback {
    /**
     * 该卡已写入用户信息
@@ -57,25 +59,29 @@
    volatile UserCard userCard;
    Handler handler;
    public LoyaltyCardReader mLoyaltyCardReader;
    /**
     * 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);
//            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);
        Intent intent = new Intent(this, getClass());
        //一旦截获NFC消息,就会通过PendingIntent调用窗口
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);
        mPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//        mLoyaltyCardReader = new LoyaltyCardReader(this);
//        if (mNfcAdapter != null) {
//            mNfcAdapter.enableReaderMode(this, mLoyaltyCardReader, READER_FLAGS, null);
//        }
    }
@@ -102,7 +108,16 @@
        //恢复默认状态
        if (mNfcAdapter != null)
            mNfcAdapter.disableForegroundDispatch(this);
//            mNfcAdapter.disableReaderMode(this);
    }
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        if (mNfcAdapter != null)
//            mNfcAdapter.disableReaderMode(this);
//    }
    /**
     * 检测工作,判断设备的NFC支持情况
@@ -120,4 +135,14 @@
        }
        return true;
    }
    @Override
    public void onAccountReceived(Tag tag) {
        Intent intent = new Intent(this, getClass());
        intent.putExtra(NfcAdapter.EXTRA_TAG, tag);
        startActivity(intent);
    }
}