android通过BroadcastReceiver监听网络和NFC状态

项目使用手机NFC读身份证,测试提bug,页面不提示NFC状态和网络连接状态。改为通过监听提示用户,mark一下:

public class MainActivity extends Activity {

    private Context mContext;
    private boolean isSupportNfc = true;
    NetWorkStateReceiver netWorkStateReceiver;
    NFCStateReceiver nfcStateReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (netWorkStateReceiver == null) {
            netWorkStateReceiver = new NetWorkStateReceiver();
        }
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(netWorkStateReceiver, filter);
//
        if (nfcStateReceiver == null) {
            nfcStateReceiver = new NFCStateReceiver();
        }
        IntentFilter nfcFilter = new IntentFilter();
        filter.addAction(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
        registerReceiver(nfcStateReceiver, filter);
        checkNFC();
        startReader();
    }

    private void startReader(){
       //读证
    }


    private void checkNFC(){
        isSupportNfc = isSupportNfc(this);
        if (!isSupportNfc) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("手机不支持NFC功能!");
            builder.create();
            builder.show();
        }
    }

    public static boolean isSupportNfc(Context context){
        PackageManager pm = context.getPackageManager();
        return pm.hasSystemFeature("android.hardware.nfc");
    }

    //NFC状态监听
    private class NFCStateReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                NfcManager manager = (NfcManager) mContext.getSystemService(Context.NFC_SERVICE);
                NfcAdapter adapter = manager.getDefaultAdapter();
                if (adapter != null) {
                    if (!adapter.isEnabled()){
                        TdrIDReader.stop(mContext);
                        mTxtWarning.setVisibility(View.VISIBLE);
                        mTxtWarning.setText("请开启NFC");
//                        Toast.makeText(mContext,"请开启NFC", Toast.LENGTH_LONG).show();
                    }else {
                        mTxtWarning.setVisibility(View.GONE);
                        startReader();
                    }
                }
            } catch (Exception e) {
                mTxtWarning.setVisibility(View.VISIBLE);
                mTxtWarning.setText("请开启NFC");
            }
        }
    }

    //网络状态监听
    private class NetWorkStateReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                ConnectivityManager connectivity = (ConnectivityManager) mContext
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connectivity != null) {
                    NetworkInfo info = connectivity.getActiveNetworkInfo();
                    if (info != null && info.isConnected()) {
                        if (info.getState() != NetworkInfo.State.CONNECTED) {
                            mTxtWarning.setVisibility(View.VISIBLE);
                            mTxtWarning.setText("您的网络未开启,请检查您的网络配置");
                        } else {
                            mTxtWarning.setVisibility(View.GONE);
                        }
                    }else {
                        mTxtWarning.setVisibility(View.VISIBLE);
                        mTxtWarning.setText("您的网络未开启,请检查您的网络配置");
                    }
                }
            } catch (Exception e) {
                mTxtWarning.setVisibility(View.VISIBLE);
                mTxtWarning.setText("您的网络未开启,请检查您的网络配置");
            }
        }
    }

}

 

注:华为手机下拉通知栏,本以为当前Activity会进入onPause()结果并没有,查资料实验最后发现:

onPause()是否调用的关键就是,是否有另一个Activity参与进来了。
而网上流传甚广的onPause()onStop()调用中提到的“遮挡”,应该修正为“被Activity遮挡”

你可能感兴趣的:(Android)