工作闲余时间学习了下其它项目中的联网方式判断,很简单的..今天有时间就放博客里面供自己记录,有需要的朋友可以同小马交流交流,希望大家多多指点。。谢谢。。
主类:
- package com.xiaoma.www;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.net.NetworkInfo.State;
- import android.net.Uri;
- import android.os.Bundle;
- import android.widget.TextView;
- /**
- * @Title: JudgeNewWayDemoActivity.java
- * @Package com.xiaoma.www
- * @Description: 判断联网方式测试类
- * @author MZH
- */
- public class JudgeNewWayDemoActivity extends Activity {
- private TextView text;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- /**
- * 下面列举几个可直接跳到联网设置的意图,供大家学习
- */
- //startActivity(new Intent(android.provider.Settings.ACTION_APN_SETTINGS));
- //startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
- //用下面两种方式设置网络
- //startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
- //startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
- init();
- }
- /**
- * 初始化实现
- */
- private void init(){
- text = (TextView)findViewById(R.id.waytext);
- /**获得系统级联网管理员对象*/
- ConnectivityManager manager = (ConnectivityManager)this.getSystemService(CONNECTIVITY_SERVICE);
- NetworkInfo info = manager.getActiveNetworkInfo();
- if(info == null){ //无网情况下
- //跳转到网络设置页面
- startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
- }else{ //有网情况下
- if(info.isAvailable()){ //网络可用时
- /**是手机自带的联网方式*/
- if(info.getType() == ConnectivityManager.TYPE_MOBILE){
- if(info.getState() == State.CONNECTED){
- text.setText(String.valueOf("手机网络可用并已连接"+"\n"+"连接网络方式为:\n"+info.getType()+",MOBILE"));
- }
- /**WIFI联网方式*/
- }else{
- text.setText(String.valueOf("连接网络方式为:"+info.getType()+",WI-FI"));
- startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
- }
- }else{
- text.setText(String.valueOf("手机网络不可用"));
- }
- }
- }
- /**
- * 临时写来练练的方法,大家可以跳过
- */
- private void kkk(){
- Uri uri = Uri.parse("content://telephony/carriers");
- Cursor cr = getContentResolver().query(uri, null, null, null, null);
- while(cr!=null && cr.moveToNext()){
- // APN id
- String id = cr.getString(cr.getColumnIndex("_id"));
- // APN name
- String apn = cr.getString(cr.getColumnIndex("apn"));
- // do other things...
- }
- }
- /**
- * 移动或联通ctwap cmwap 判断 这个方法朋友们可根据自己的需要使用,小马
- * 只是记在这个地方,供自己记录的,有需要的朋友可以交流讨论下..吼吼
- */
- @SuppressWarnings("unused")
- private void initWapState() {
- //ConnectivityManager: Class that answers queries about the state of network connectivity.
- ConnectivityManager conMan = (ConnectivityManager)
- getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo netInfo = conMan.getActiveNetworkInfo();
- if (netInfo != null) {
- String extraInfo = netInfo.getExtraInfo();//Report the extra information about the network state
- if (extraInfo != null) {
- if (extraInfo.equals("cmwap")) {
- }else if(extraInfo.equals("ctwap") || extraInfo.equals("#777")){
- }
- }
- }
- }
- }
WIFI控制类:
- package com.xiaoma.www;
- import java.util.List;
- import android.content.Context;
- import android.net.wifi.ScanResult;
- import android.net.wifi.WifiConfiguration;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.net.wifi.WifiManager.WifiLock;
- /**
- * @Title: WifiCheckUtil.java
- * @Package com.lg.lvguo.util
- * @Description: WIFI控制类
- * @author MZH
- * @version FG1.0
- */
- public class WifiCheckUtil {
- //定义WifiManager对象
- private WifiManager mWifiManager;
- //定义WifiInfo对象
- private WifiInfo mWifiInfo;
- //扫描出的网络连接列表
- private List<ScanResult> mWifiList;
- //网络连接列表
- private List<WifiConfiguration> mWifiConfiguration;
- //定义一个WifiLock
- WifiLock mWifiLock;
- //构造器
- public WifiCheckUtil(Context context)
- {
- //取得WifiManager对象
- mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
- //取得WifiInfo对象
- mWifiInfo = mWifiManager.getConnectionInfo();
- }
- //取到WIFI状态
- public int checkWifiState(){
- return mWifiManager.getWifiState();
- }
- //打开WIFI
- public void OpenWifi()
- {
- if (!mWifiManager.isWifiEnabled())
- {
- mWifiManager.setWifiEnabled(true);
- }
- }
- //关闭WIFI
- public void CloseWifi()
- {
- if (!mWifiManager.isWifiEnabled())
- {
- mWifiManager.setWifiEnabled(false);
- }
- }
- //锁定WifiLock
- public void AcquireWifiLock()
- {
- mWifiLock.acquire();
- }
- //解锁WifiLock
- public void ReleaseWifiLock()
- {
- //判断时候锁定
- if (mWifiLock.isHeld())
- {
- mWifiLock.acquire();
- }
- }
- //创建一个WifiLock
- public void CreatWifiLock()
- {
- mWifiLock = mWifiManager.createWifiLock("Lock");
- }
- //得到配置好的网络
- public List<WifiConfiguration> GetConfiguration()
- {
- return mWifiConfiguration;
- }
- //指定配置好的网络进行连接
- public void ConnectConfiguration(int index)
- {
- //索引大于配置好的网络索引返回
- if(index > mWifiConfiguration.size())
- {
- return;
- }
- //连接配置好的指定ID的网络
- mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId, true);
- }
- public void StartScan()
- {
- mWifiManager.startScan();
- //得到扫描结果
- mWifiList = mWifiManager.getScanResults();
- //得到配置好的网络连接
- mWifiConfiguration = mWifiManager.getConfiguredNetworks();
- }
- //得到网络列表
- public List<ScanResult> GetWifiList()
- {
- return mWifiList;
- }
- //查看扫描结果
- public StringBuilder LookUpScan()
- {
- StringBuilder stringBuilder = new StringBuilder();
- for (int i = 0; i < mWifiList.size(); i++)
- {
- stringBuilder.append("Index_"+new Integer(i + 1).toString() + ":");
- //将ScanResult信息转换成一个字符串包
- //其中把包括:BSSID、SSID、capabilities、frequency、level
- stringBuilder.append((mWifiList.get(i)).toString());
- stringBuilder.append("/n");
- }
- return stringBuilder;
- }
- //得到MAC地址
- public String GetMacAddress()
- {
- return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
- }
- //得到接入点的BSSID
- public String GetBSSID()
- {
- return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
- }
- //得到IP地址
- public int GetIPAddress()
- {
- return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
- }
- //得到连接的ID
- public int GetNetworkId()
- {
- return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
- }
- //得到WifiInfo的所有信息包
- public String GetWifiInfo()
- {
- return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
- }
- //添加一个网络并连接
- public void AddNetwork(WifiConfiguration wcg)
- {
- int wcgID = mWifiManager.addNetwork(wcg);
- mWifiManager.enableNetwork(wcgID, true);
- }
- //断开指定ID的网络
- public void DisconnectWifi(int netId)
- {
- mWifiManager.disableNetwork(netId);
- mWifiManager.disconnect();
- }
- }
全局配置文件:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.xiaoma.www"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="15" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".JudgeNewWayDemoActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- </manifest>
小DEMO源码小马放附件里面了,以备自己注意丢了,呵呵,有需要的朋友可下载下的,一起学!
本文转自华华世界 51CTO博客,原文链接:http://blog.51cto.com/mzh3344258/791541,如需转载请自行联系原作者