`
ET焖猪仔
  • 浏览: 233552 次
  • 性别: Icon_minigender_1
  • 来自: 广东
社区版块
存档分类
最新评论

判断现在的网络状态

阅读更多
用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方式连接");
        

    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics