ET焖猪仔 发表于 2013-1-15 02:36:50

判断现在的网络状态

用ConnectivityManager可以判断现在的网络是wifi还是GPRS~
package com.et.TestNetWork;import android.app.Activity;import android.content.Context;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.os.Bundle;import android.widget.TextView;public class TestNetWork extends Activity {    /** Called when the activity is first created. */private TextView text;public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.main);      text = (TextView) findViewById(R.id.text);      ConnectivityManager connec =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);      if (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)      text.setText("wifi方式连接");      if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)      text.setText("GPRS方式连接");            }}
页: [1]
查看完整版本: 判断现在的网络状态