android判断移动网络是不是打开

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android判断移动网络是不是打开相关的知识,希望对你有一定的参考价值。

package com.shanghaizhida.mtrader.util;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class InterentConnection
Context context;
public InterentConnection(Context context)

this.context=context;

public boolean isConn()

boolean bisConnFlag=false;
ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = conManager.getActiveNetworkInfo();
if(network!=null)
bisConnFlag=conManager.getActiveNetworkInfo().isAvailable();

return bisConnFlag;


public void setNetworkMethod()
//提示对话框
AlertDialog.Builder builder=new Builder(context);
builder.setTitle("网络设置提示").setMessage("网络连接不可用,是否进行设置?(取消则退出程序)").setPositiveButton("设置", new DialogInterface.OnClickListener()

@Override
public void onClick(DialogInterface dialog, int which)
Intent intent=null;
//判断手机系统的版本 即API大于10 就是3.0或以上版本
if(android.os.Build.VERSION.SDK_INT>10)
intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
else
intent = new Intent();
ComponentName component = new ComponentName("com.android.settings","com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");

context.startActivity(intent);
dialog.dismiss();

).setNegativeButton("取消", new DialogInterface.OnClickListener()

@Override
public void onClick(DialogInterface dialog, int which)
dialog.dismiss();
System.exit(0);

).show();


参考技术A   Android 判断网络状态这一应用技巧在实际应中是比较重要的。那么,在Android操作系统中,如何能够正确的判断我们所连接的网络是否断开:
  public class ConnectionChangeReceiver extends
  BroadcastReceiver
  
  @Override
  public void onReceive( Context context, Intent intent )
  
  ConnectivityManager connectivityManager =
  (ConnectivityManager) context.getSystemService
  ( Context.CONNECTIVITY_SERVICE );
  NetworkInfo activeNetInfo = connectivityManager.
  getActiveNetworkInfo();
  NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
  ( ConnectivityManager.TYPE_MOBILE );
  if ( activeNetInfo != null )
  
  Toast.makeText( context, "Active Network Type : " +
  activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
  
  if( mobNetInfo != null )
  
  Toast.makeText( context, "Mobile Network Type : " +
  mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
  
  
  
  < !-- Needed to check when the network connection changes -->
  < uses-permission android:name="android.permission.
  ACCESS_NETWORK_STATE"/>
  < receiver android:name="com.blackboard.androidtest.
  receiver.ConnectionChangeReceiver"
  android:label="NetworkConnection">
  < intent-filter>
  < action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
  < /intent-filter>
  < /receiver>

如何使用js判断当前页面是pc还是移动端打开的

1.利用了正则表达式三目运算符,含义就是如果是移动端打开的话那就跳转到 "https:www.baidu.com/" ,如果不是就跳转到"http://new.baidu.com/"

window.location.href = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ? "https://www.baidu.com/" :  "http://news.baidu.com/";

  等同于

if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
    window.location.href = "https://www.baidu.com/";
} else {
    window.location.href = "http://news.baidu.com/";
}

2.第二种方法

function IsPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone",
                "SymbianOS", "Windows Phone",
                "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

3.第三种方法

function browserRedirect() {
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
        window.location.href=B页面;
    }
}
browserRedirect();

4.百度判断的方法

function uaredirect(f) {
 try {
  if (document.getElementById("bdmark") != null) {
   return
  }
  var b = false;
  if (arguments[1]) {
   var e = window.location.host;
   var a = window.location.href;
   if (isSubdomain(arguments[1], e) == 1) {
    f = f + "/#m/" + a;
    b = true
   } else {
    if (isSubdomain(arguments[1], e) == 2) {
     f = f + "/#m/" + a;
     b = true
    } else {
     f = a;
     b = false
    }
   }
  } else {
   b = true
  }
  if (b) {
   var c = window.location.hash;
   if (!c.match("fromapp")) {
    if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|SymbianOS)/i))) {
     location.replace(f)
    }
   }
  }
 } catch(d) {}
}
function isSubdomain(c, d) {
 this.getdomain = function(f) {
  var e = f.indexOf("://");
  if (e > 0) {
   var h = f.substr(e + 3)
  } else {
   var h = f
  }
  var g = /^www\./;
  if (g.test(h)) {
   h = h.substr(4)
  }
  return h
 };
 if (c == d) {
  return 1
 } else {
  var c = this.getdomain(c);
  var b = this.getdomain(d);
  if (c == b) {
   return 1
  } else {
   c = c.replace(".", "\\.");
   var a = new RegExp("\\." + c + "$");
   if (b.match(a)) {
    return 2
   } else {
    return 0
   }
  }
 }
};

以上是百度总结的,方便自己罢了!

 

以上是关于android判断移动网络是不是打开的主要内容,如果未能解决你的问题,请参考以下文章

转:Android 判断用户2G/3G/4G移动数据网络

如何判断自己家的宽带是不是有公网IP?

解决4G网络移动打不开网站,WiFi可以正常访问

js-获取用户移动端网络类型:wifi4g3g2g...

Android 网络连接判断与处理

打开移动数据时如何从wifi网络访问本地url?