zl程序教程

您现在的位置是:首页 >  APP

当前栏目

[android] 网络链接类型和渠道

2023-02-18 15:47:14 时间

1.实现方式

1.1使用HttpUrlConnection

1.2使用HttpClient

1.3使用Socket,比如:豌豆荚,聊天工具

 

2.通讯渠道

2.1 WLAN(wi-fi)100米左右的数据传输

2.2 手机APN接入点(基站)

2.2.1 wap的方式,中国特色,首先会连接电信运营商代理拦截10.0.0.172HttpUrlConnection会不稳定

2.2.2 net的方式

 

3.通讯工具

3.1判断网络类型

根据Context上下文,判断是wifi还是APN,然后再判断APN的接入方式,有代理信息的是wap没有的是net

package com.tsh.lottery.net;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;

public class NetUtils {
    /**
     * 获取网络状态
     * @param context
     * @return
     */
    public static String getNetworkInfo(Context context) {
        ConnectivityManager cm=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //判断wifi
        NetworkInfo networkInfo=cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if(networkInfo!=null && networkInfo.isConnected()){
            return "wifi";
        }
        //判断APN
        NetworkInfo mobileInfo=cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if(mobileInfo!=null && mobileInfo.isConnected()){
            //获取APN接入方式,因为权限问题,没有成功
//            ContentResolver resolver=context.getContentResolver();
//            Cursor cursor=resolver.query(Uri.parse("content://telephony/carriers"), null, null, null, null);
//            if(cursor!=null && cursor.moveToFirst()){
//                String proxy=cursor.getString(cursor.getColumnIndex("proxy"));
//                System.out.println(proxy);
//            }
            return "mobile";
        }
        return "no";
    }
}