android判断网络状态

判断android的网络状态

手机网络,WIFI,蓝牙等


需要权限


    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />


package com.qefee.testnetwork;

import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	public static String TAG = "MainActivity_TAG";

	TextView textView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		textView = (TextView) findViewById(R.id.msg);
		
		ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(
                Context.CONNECTIVITY_SERVICE);
        State type_bluetooth = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH).getState();
//        State type_dummy = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_DUMMY).getState();
//        State type_ethernet = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET).getState();
        State type_mobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
//        State type_mobile_dun = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_DUN).getState();
//        State type_mobile_hipri = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
//        State type_mobile_mms = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS).getState();
//        State type_mobile_supl = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_SUPL).getState();
        State type_wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
//        State type_wimax = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIMAX).getState();
        
        Log.d(TAG, "type_bluetooth = "+type_bluetooth.name());
//        Log.d(TAG, "type_dummy = "+type_dummy.name());
//        Log.d(TAG, "type_ethernet = "+type_ethernet.name());
        Log.d(TAG, "type_mobile = "+type_mobile.name());
//        Log.d(TAG, "type_mobile_dun = "+type_mobile_dun.name());
//        Log.d(TAG, "type_mobile_hipri = "+type_mobile_hipri.name());
//        Log.d(TAG, "type_mobile_mms = "+type_mobile_mms.name());
//        Log.d(TAG, "type_mobile_supl = "+type_mobile_supl.name());
        Log.d(TAG, "type_wifi = "+type_wifi.name());
//        Log.d(TAG, "type_wimax = "+type_wimax.name());
        
		String msg = "type_bluetooth = " + type_bluetooth.name() + "\r\n";
		msg += "type_mobile = " + type_mobile.name() + "\r\n";
		msg += "type_wifi = " + type_wifi.name();
		
		textView.setText(msg);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}


参考至

http://www.111cn.net/sj/android/41946.htm

你可能感兴趣的:(android判断网络状态)