检查手机是否连接网络,sim的存在以及是否支持NFC功能

代码如下:

/**
     * 检查sim卡是否存在
     * @return
     */
    private boolean checkPhoneNet(){
          TelephonyManager mTelephonyManager=(TelephonyManager) this.getSystemService(Service.TELEPHONY_SERVICE);
          if(mTelephonyManager.getSimState()!=TelephonyManager.SIM_STATE_READY) //SIM卡没有就绪
          {
              MessageBox.alert(getApplicationContext(), "SIM卡没有就绪");
              return false;
          }else{
           ConnectivityManager cManager=(ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
           NetworkInfo info = cManager.getActiveNetworkInfo();
             if (info != null && info.isAvailable()){         
                  //能联网
//               MessageBox.alert(getApplicationContext(), "能联网");
                   return true;
             }else{
                  //do something
                 MessageBox.alert(getApplicationContext(), "不能联网");
                  //不能联网
              return false;
             }
          }
         }

    /**
     * 判断手机是否支持NFC功能和OpenMobileAPI
     * */
    private boolean isSupportMocam() {
        boolean isSupportMocam = false;
        // 判断手机是否有NFC功能
        PackageManager pm = getPackageManager();
        boolean hasNfcFeature = pm.hasSystemFeature(PackageManager.FEATURE_NFC);
        // 如果手机支持NFC,尝试能否加载OpenMobile API的类
        if (hasNfcFeature) {
            try {
                Class.forName("org.simalliance.openmobileapi.SEService");
                isSupportMocam = true;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                MessageBox.alert(getApplicationContext(), "加载OpenMobile失败");
                Log.d(TAG,"Class not Found:org.simalliance.openmobileapi.SEService");
            }
        }else{
            MessageBox.alert(getApplicationContext(), "您的手机不支持NFC");
        }

        return isSupportMocam;
    }

    /**
     * 判断NFC是否打开
     * 
     * @return
     */
    public boolean hasNFC(){
         NfcManager manager = (NfcManager) this.getSystemService(Context.NFC_SERVICE);  
         NfcAdapter adapter = manager.getDefaultAdapter();
         if (adapter != null && adapter.isEnabled()) {  
                // adapter存在,能启用  
//           Toast.makeText(getApplicationContext(), "您的手机NFC已打开", 1).show();   
                return true;  
          }
         MessageBox.alert(getApplicationContext(), "您的手机没有打开NFC");
        return false;
    }

注释:MessageBox.alert是一个表示弹出对话框,一个封装工具

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值