左晓为主开发手持机充值管理机
zuoxiao
2023-11-23 50f6dd3b617f769e7fc6094c2dd0752747541489
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.dayu.recharge.activity;
 
/**
 * Created by zuo on 2018/12/2.
 */
 
 
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.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.
 * Email:584182977@qq.com
 * Description:
 * 子类在onNewIntent方法中进行NFC标签相关操作。
 * launchMode设置为singleTop或singelTask,保证Activity的重用唯一
 * 在onNewIntent方法中执行intent传递过来的Tag数据
 * 将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
     * 启动Activity,界面可见时.
     */
    @Override
    protected void onStart() {
        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);
 
    }
 
    /**
     * 获得焦点,按钮可以点击
     */
    @Override
    public void onResume() {
        super.onResume();
        if (!ifNFCUse(this)) {
            this.finish();
        }
        //设置处理优于所有其他NFC的处理
        if (mNfcAdapter != null)
            mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
    }
 
    /**
     * 暂停Activity,界面获取焦点,按钮可以点击
     */
    @Override
    public void onPause() {
        super.onPause();
        //恢复默认状态
        if (mNfcAdapter != null)
            mNfcAdapter.disableForegroundDispatch(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;
    }
}