利用Java反射机制控制Nfc开关

来源:互联网 发布:linux 图片压缩像素 编辑:程序博客网 时间:2024/05/16 09:37

利用Java反射机制控制Nfc开关

Nfc状态广播说明

    /**     * Used as an int extra field in {@link #ACTION_ADAPTER_STATE_CHANGED}     * intents to request the current power state. Possible values are:     * {@link #STATE_OFF},     * {@link #STATE_TURNING_ON},     * {@link #STATE_ON},     * {@link #STATE_TURNING_OFF},     */    public static final String EXTRA_ADAPTER_STATE = "android.nfc.extra.ADAPTER_STATE";    public static final int STATE_OFF = 1;    public static final int STATE_TURNING_ON = 2;    public static final int STATE_ON = 3;    public static final int STATE_TURNING_OFF = 4;    /**     * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.     * <p>     * Setting this flag enables polling for Nfc-A technology.     */

Nfc状态广播action为“android.nfc.action.ADAPTER_STATE_CHANGED”,在这里他会传递四种int类型的状态值,其中STATE_OFF为关闭完成状态,STATE_ON为准备就绪状态,STATE_TURNING_ON为启动Nfc,STATE_TURNING_OFF为准备关闭状态。

编写BroadcastReceiver来接收Nfc状态

mReceiver = new BroadcastReceiver() {            @Override            public void onReceive(Context context, Intent intent) {                Log.d(TAG, "接收成功");                String action = intent.getAction();                if (NfcAdapter.ACTION_ADAPTER_STATE_CHANGED.equals(action)) {                    getStatus(intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_OFF));                }            }            private void getStatus(int intExtra) {                switch (intExtra) {                    case NfcAdapter.STATE_OFF:                        Message msg_off = new Message();                        msg_off.obj = "close";                        mHandler.sendMessage(msg_off);                        break;                    case NfcAdapter.STATE_ON:                        Message msg_on = new Message();                        msg_on.obj = "open";                        mHandler.sendMessage(msg_on);                        break;                    case NfcAdapter.STATE_TURNING_OFF:                        Message msg_turning_off = new Message();                        msg_turning_off.obj = "turning off";                        mHandler.sendMessage(msg_turning_off);                        break;                    case NfcAdapter.STATE_TURNING_ON:                        Message msg_turning_on = new Message();                        msg_turning_on.obj = "turning on";                        mHandler.sendMessage(msg_turning_on);                        break;                }                String a = intExtra + "";                Log.d(TAG, a);            }        };        IntentFilter intentFilter = new IntentFilter("android.nfc.action.ADAPTER_STATE_CHANGED");        registerReceiver(mReceiver, intentFilter);

接收成功后,通知handler来更新ui界面,把Nfc状态显示出来。

利用Java反射机制控制Nfc开关

控制Nfc开关的方法是android.nfc.NfcAdapter里面的enable()和disable();在Android API中代码如下:

    /**     * Enable NFC hardware.     *     * <p>This call is asynchronous. Listen for     * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the     * operation is complete.     *     * <p>If this returns true, then either NFC is already on, or     * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent     * to indicate a state transition. If this returns false, then     * there is some problem that prevents an attempt to turn     * NFC on (for example we are in airplane mode and NFC is not     * toggleable in airplane mode on this platform).     *     * @hide     */    @SystemApi    public boolean enable() {        try {            return sService.enable();        } catch (RemoteException e) {            attemptDeadServiceRecovery(e);            return false;        }    }    /**     * Disable NFC hardware.     *     * <p>No NFC features will work after this call, and the hardware     * will not perform or respond to any NFC communication.     *     * <p>This call is asynchronous. Listen for     * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the     * operation is complete.     *     * <p>If this returns true, then either NFC is already off, or     * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent     * to indicate a state transition. If this returns false, then     * there is some problem that prevents an attempt to turn     * NFC off.     *     * @hide     */    @SystemApi    public boolean disable() {        try {            return sService.disable(true);        } catch (RemoteException e) {            attemptDeadServiceRecovery(e);            return false;        }    }

因为在方法注释上面有@hide,说明这两个方法是API隐藏起来的,如果想要调用这两个方法控制Nfc开关就要用到Java反射,代码如下:

    private void nfcEnable() {        try {            Class clazz = Class.forName("android.nfc.NfcAdapter");            Method enable = clazz.getMethod("enable");            // enable.setAccessible(true);            enable.invoke(adapter);            Log.d(TAG, "调用enable方法成功");        } catch (Exception e) {            e.printStackTrace();        }    }    private void nfcDisable() {        try {            Class clazz = Class.forName("android.nfc.NfcAdapter");            Method enable = clazz.getMethod("disable", boolean.class);            // enable.setAccessible(true);            enable.invoke(adapter, false);            Log.d(TAG, "调用disable方法成功");        } catch (Exception e) {            e.printStackTrace();        }    }

在支持Nfc的手机上调用这两个方法,可以观察手机Nfc设置中的Nfc开关,会随之而改变,需求实现!

1 0