美文网首页
通过广播对实时网络权限的判断

通过广播对实时网络权限的判断

作者: 仁昌居士 | 来源:发表于2017-04-24 13:30 被阅读0次

    通过广播对实时网络权限的判断

    private boolean register = false;
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
           State wifiState = null;
           State mobileState = null;
           ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
           wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
           mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
           if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED == mobileState) {
               // 手机网络连接成功
               new MaterialDialog.Builder(context)
                       .title("网络选择")
                       .content("您当前处于非wifi网络环境下,继续使用可能产生流量")
                       .positiveText("继续")
                       .negativeText("取消")
                       .positiveColor(getResources().getColor(R.color.colorPrimary))
                       .negativeColor(getResources().getColor(R.color.colorPrimary))
                       .onPositive(new MaterialDialog.SingleButtonCallback() {
                           @Override
                           public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                               streamType = "fluxstream";
                               mPresenter.StartRealPlay(m_svPlayer, m_nPort, m_szCameraId, m_pDLLHandle, mTimeOut);
                               mPresenter.startGetStreamRate();
                           }
                       })
                       .show();
           } else if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED != mobileState) {
               // 手机没有任何的网络
               UIHelper.showtoast(context, "无网络,请检查网络!");
           } else if (wifiState != null && State.CONNECTED == wifiState) {
               // 无线网络连接成功
    
           }
       }
    };
    
    private void registerNetworkReceiver() {
       if(!register) {
           IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
           this.registerReceiver(mReceiver, filter);
           register = true;
       }
    }
    
    private void unRegisterNetworkReceiver() {
       if(register) {
           this.unregisterReceiver(mReceiver);
           register = false;
       }
    }
    

    相关文章

      网友评论

          本文标题:通过广播对实时网络权限的判断

          本文链接:https://www.haomeiwen.com/subject/skqpzttx.html