GPRS连接相关

来源:互联网 发布:反向域名解析 阿里云 编辑:程序博客网 时间:2024/06/06 12:42

http://www.eoeandroid.com/thread-27682-1-1.html

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

    }

}

 

复制代码

package zy.netmana;

import android.app.Activity;

import android.content.Context;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.os.Bundle;

import android.widget.TextView;

import android.widget.Toast;

 

public class main extends Activity

{

    /** Called when the activity is first created. */

    @Override public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        String display="";

        ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo info = connec.getActiveNetworkInfo();

        connec.getAllNetworkInfo();

        TextView tv=(TextView)findViewById(R.id.tv);

        String typeName = info.getTypeName(); //cmwap/cmnet/wifi/uniwap/uninet

        NetworkInfo []allinfo= connec.getAllNetworkInfo(); tv.setText( allinfo[0].toString());

    }

}

 

 复制代码 网络连接类型

ConnectivityManager getActiveNetworkInfo().getType()==ConnectivityManager.TYPE_WIFI/TYPE_MOBILE

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

android 中查看当前是否联网 方法如下:

ConnectivityManager cManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cwjManager.getActiveNetworkInfo();

if (info != null && info.isAvailable())

{

    //do something //能联网

    return true;

}

else

{

     //do something //不能联网

     return false;

}

如果为True则表示当前Android手机已经联网,可能是WiFi或GPRS、HSDPA等等,具体的可以通过 ConnectivityManager 类的getActiveNetworkInfo() 方法判断详细的接入方式。 同时要在manifest里面加个权限 文档如下:

boolean android.net.NetworkInfo.isAvailable()

public boolean isAvailable () Indicates whether network connectivity is possible. A network is unavailable when a persistent or semi-persistent condition prevents the possibility of connecting to that network. Examples include The device is out of the coverage area for any network of this type. The device is on a network other than the home network (i.e., roaming), and data roaming has been disabled. The device's radio is turned off, e.g., because airplane mode is enabled. Returns true if the network is available, false otherwise

原创粉丝点击