码迷,mamicode.com
首页 > 移动开发 > 详细

Android判断网络是否连接,跳转到设置页面以及判断连接的是什么网络

时间:2014-09-24 14:46:57      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:android   网络   判断网络   网络应用   

如果拟开发一个网络应用的程序,首先考虑是否接入网络,在Android手机中判断是否联网可以通过ConnectivityManager 类的isAvailable()方法判断,首先获取网络通讯类的实例 ConnectivityManager cwjManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);  ,使用cwjManager.getActiveNetworkInfo().isAvailable(); 来返回是否有效,如果为True则表示当前Android手机已经联网,可能是WiFi或GPRS、HSDPA等等,具体的可以通过ConnectivityManager 类的getActiveNetworkInfo() 方法判断详细的接入方式,需要注意的是有关调用需要加入<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>  这个权限,android开发网提醒大家在真机上Market和Browser程序都使用了这个方法,来判断是否继续,同时在一些网络超时的时候也可以检查下网络连接是否存在,以免浪费手机上的电力资源

判断网络是否连接资源地址:http://download.csdn.net/detail/u014649598/7966901

 

 

一、判断网络连接是否可用,不可用跳转到设置界面

@Override
    protected void onStart() {
    	AlertDialog.Builder builder=new Builder(this);
    	
				//检查用户的网络情况
		        ConnectivityManager cm=(ConnectivityManager) MainActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
		        //返回當前可用網絡信息
		        
		        NetworkInfo info=cm.getActiveNetworkInfo();
		        if(info!=null&&info.isConnected()){
		        	Toast.makeText(MainActivity.this, "网络可用", 0).show();
		        	
		        }else{
		        	Toast.makeText(MainActivity.this, "网络不可用", 0).show();
		        	builder.setTitle("提醒");
		        	builder.setMessage("当前网络不可用,点击确定设置网络");
		        	builder.setPositiveButton("确定", new OnClickListener() {
		    			
		    			@Override
		    			public void onClick(DialogInterface dialog, int which) {
		        	Intent intent=new Intent();
		        	intent.setClassName("com.android.settings", "com.android.settings.Settings");
		        	startActivity(intent);
		        }
			}
		);
		        	builder.create().show();
		        }
    	
    	super.onStart();
    }


二、使用ConnectionChangeReceiver判断网络是否可用

 

public class MainActivity extends Activity {
	private ConnectionChangeReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //用广播判断
        myReceiver=new ConnectionChangeReceiver();
		this.registerReceiver(myReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));  
    }
public class ConnectionChangeReceiver extends BroadcastReceiver
    {
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		ConnectivityManager connectivityManager = (ConnectivityManager)
    				context.getSystemService(Context.CONNECTIVITY_SERVICE);
    		NetworkInfo mobworkInfo = connectivityManager
    				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    		NetworkInfo wifiworkInfo = connectivityManager
    				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    		boolean IsNetConnected = mobworkInfo.isConnected();
    		boolean IsWifiConnected = wifiworkInfo.isConnected();
    		if(IsNetConnected || IsWifiConnected){
    			System.out.println("网络打开");
    			Log.e("wankai", "wankai");
    			boolean isConnected = true;			
    		}else{
    			boolean isConnected = false;
    			System.out.println("网络关闭");	
    			Log.e("wankai2", "wangguan");
    		}
    	}		
    }


}
  

三、判断GPS是否打开

 public static boolean isGpsEnabled(Context context) {   
        LocationManager lm = ((LocationManager) context   
                .getSystemService(Context.LOCATION_SERVICE));   
        List<String> accessibleProviders = lm.getProviders(true);   
        return accessibleProviders != null && accessibleProviders.size() > 0;   
    } 


 

四、判断WIFI是否打开

public static boolean isWifiEnabled(Context context) {   
        ConnectivityManager mgrConn = (ConnectivityManager) context   
                .getSystemService(Context.CONNECTIVITY_SERVICE);   
        TelephonyManager mgrTel = (TelephonyManager) context   
                .getSystemService(Context.TELEPHONY_SERVICE);   
        return ((mgrConn.getActiveNetworkInfo() != null && mgrConn   
                .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel   
                .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);   
    } 


 

五、判断是否是3G网络

public static boolean is3rd(Context context) {   
        ConnectivityManager cm = (ConnectivityManager) context   
                .getSystemService(Context.CONNECTIVITY_SERVICE);   
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();   
        if (networkINfo != null   
                && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {   
            return true;   
        }   
        return false;   
    }  


 

六、判断是wifi还是3g网络,用户的体现性在这里了,wifi就可以建议下载或者在线播放。

public static boolean isWifi(Context context) {   
            ConnectivityManager cm = (ConnectivityManager) context   
                    .getSystemService(Context.CONNECTIVITY_SERVICE);   
            NetworkInfo networkINfo = cm.getActiveNetworkInfo();   
            if (networkINfo != null   
                    && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {   
                return true;   
            }   
            return false;   
        }


 

 

 

 

 

 

Android判断网络是否连接,跳转到设置页面以及判断连接的是什么网络

标签:android   网络   判断网络   网络应用   

原文地址:http://blog.csdn.net/u014649598/article/details/39520629

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!