开发背景:电视应用必须兼容公司过去已经发行的4.4盒子,除了文件管理,wifi,有线,蓝牙所有用到6.0系统jar的功能全都不能用,4.4系统不能提供对应jar包给我,具体原因不明
开发需求:获取ip地址,子网掩码,默认网关,dns服务器:
4.4有线网络是有开关功能的,而6.0是没有的。因此需要判定当前以太网是否打开,如果没打开就让他打开
if (!isEthernetEnable()){
mEthernetManager.setEthernetEnabled(true);
}
private boolean isEthernetEnable() {
boolean isEthernetEnable = false;
if (mEthernetManager == null) {
} else {
isEthernetEnable = (mEthernetManager.getEthernetIfaceState() == EthernetDataTracker.ETHER_IFACE_STATE_UP);
}
Log.i(TAG, "当前有线是否开启:" + isEthernetEnable);
return isEthernetEnable;
}
package com.ygjy.setting4_0;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.EthernetDataTracker;
import android.net.ethernet.EthernetManager;
import android.os.SystemProperties;
import android.provider.Settings1;
import android.util.Log;
/**
* @创建人:hcy
* @创建时间:2018/11/6
* @作用描述:Function 4.4有线的工具类
**/
public class EthernetHelper {
private EthernetManager mEthernetManager;
private Context mContext;
private BroadcastReceiver mReceiver;
private static final String TAG = "Ethernet4_4Helper:>>";
private ContentResolver mContentResolver;
public boolean getConnectMode() {
return getNetState()==Net_State.dynamic_state;
}
private void unRegisterEthernetReceiver(Context context) {
if (null != mReceiver) {
context.unregisterReceiver(mReceiver);
}
}
public void recycler(Context context) {
unRegisterEthernetReceiver(context);
}
private enum Net_State {
dynamic_state, static_state;
}
public EthernetHelper(Context mContext) {
this.mContext = mContext;
mContentResolver = mContext.getContentResolver();
mEthernetManager = (EthernetManager) mContext.getSystemService("ethernet");
registerEthernetReceiver();
}
public void registerEthernetReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(EthernetDataTracker.ETHERNET_STATE_CHANGED_ACTION);
if (mReceiver == null) {
mReceiver = new EthernetReceiver();
}
mContext.registerReceiver(mReceiver, filter);
}
private class EthernetReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(EthernetDataTracker.ETHERNET_STATE_CHANGED_ACTION)) {
// 取得当前以太网状态
int state = intent.getIntExtra(EthernetDataTracker.EXTRA_ETHERNET_STATE, 0);
switch (state) {
case EthernetDataTracker.ETHER_STATE_CONNECTED: //已连接
Log.e(TAG, "有线连接成功");
if (null != mCallback) {
mCallback.onConnected(getIP(), getMask(), getGateway(), getDns1(), getDns2());
}
break;
case EthernetDataTracker.ETHER_STATE_CONNECTING:
Log.e(TAG, "有线连接中");
if (null != mCallback) {
mCallback.onConnecting();
}
break;
case EthernetDataTracker.ETHER_STATE_DISCONNECTED: //未连接
Log.e(TAG, "有线断开");
if (null != mCallback) {
mCallback.onDisconnected(mNullInfo, mNullInfo, mNullInfo, mNullInfo, mNullInfo);
}
break;
}
}
}
}
//空
private String mNullInfo = "0.0.0.0";
/*ip
*/
public String getIP() {
Net_State netState = getNetState();
if (netState == Net_State.static_state) {
return getStaticIp();
} else if (netState == Net_State.dynamic_state) {
return getDHCPIP();
} else {
return mNullInfo;
}
}
//子网掩码
public String getMask() {
Net_State netState = getNetState();
if (netState == Net_State.dynamic_state) {
return getDHCPMask();
} else if (netState == Net_State.static_state) {
return getStaticMask();
} else {
return mNullInfo;
}
}
//网关
public String getGateway() {
Net_State netState = getNetState();
if (netState == Net_State.dynamic_state) {
return getDHCPGateway();
} else if (netState == Net_State.static_state) {
return getStaticGateway();
} else {
return mNullInfo;
}
}
public String getDns1() {
Net_State netState = getNetState();
if (netState == Net_State.dynamic_state) {
return getDHCPDns1();
} else if (netState == Net_State.static_state) {
return getStaticDns1();
} else {
return mNullInfo;
}
}
public String getDns2() {
Net_State netState = getNetState();
if (netState == Net_State.dynamic_state) {
return getDHCPDns2();
} else if (netState == Net_State.static_state) {
return getStaticDns2();
} else {
return mNullInfo;
}
}
private String getStaticDns2() {
String mEthdns2 = Settings1.System.getString(mContentResolver,
Settings1.System.ETHERNET_STATIC_DNS2);
Log.e(TAG, "getStaticDns2: 静态DNS2" + mEthdns2);
return mEthdns2;
}
private String getDHCPDns2() {
String iface = mEthernetManager.getEthernetIfaceName();
String tempDNS2Info = SystemProperties.get("dhcp." + iface + ".dns2");
if ((tempDNS2Info != null) && (!tempDNS2Info.equals(""))) {
Log.e(TAG, "getDHCPDns2: 动态DNS2:" + tempDNS2Info);
return tempDNS2Info;
} else {
Log.e(TAG, "getDHCPDns2: 动态DNS2:" + mNullInfo);
return mNullInfo;
}
}
private String getStaticDns1() {
String mEthdns1 = Settings1.System.getString(mContentResolver,
Settings1.System.ETHERNET_STATIC_DNS1);
Log.e(TAG, "getStaticDns1: 静态dns1::" + mEthdns1);
return mEthdns1;
}
private String getDHCPDns1() {
String iface = mEthernetManager.getEthernetIfaceName();
String tempDNS1Info = SystemProperties.get("dhcp." + iface + ".dns1");
if ((tempDNS1Info != null) && (!tempDNS1Info.equals(""))) {
Log.e(TAG, "getDHCPDns1: 动态DNS1::" + tempDNS1Info);
return tempDNS1Info;
} else {
Log.e(TAG, "getDHCPDns1: 动态DNS1::" + mNullInfo);
return mNullInfo;
}
}
private String getStaticGateway() {
String mEthGateway = Settings1.System.getString(mContentResolver,
Settings1.System.ETHERNET_STATIC_GATEWAY);
Log.e(TAG, "getStaticGateway: 静态网关" + mEthGateway);
return mEthGateway;
}
private String getDHCPGateway() {
// 取得动态网关
String iface = mEthernetManager.getEthernetIfaceName();
String tempIpInfo = SystemProperties.get("dhcp." + iface + ".gateway");
if ((tempIpInfo != null) && (!tempIpInfo.equals(""))) {
Log.e(TAG, "getDHCPGateway: 动态网关" + tempIpInfo);
return tempIpInfo;
} else {
Log.e(TAG, "getDHCPGateway: 动态网关" + mNullInfo);
return mNullInfo;
}
}
private String getStaticMask() {
String mEthNetmask = Settings1.System.getString(mContentResolver,
Settings1.System.ETHERNET_STATIC_NETMASK);
Log.e(TAG, "getStaticMask: 静态子网掩码" + mEthNetmask);
return mEthNetmask;
}
private String getDHCPMask() {
// 取得动态子网掩码
String iface = mEthernetManager.getEthernetIfaceName();
String tempMaskInfo = SystemProperties.get("dhcp." + iface + ".mask");
if ((tempMaskInfo != null) && (!tempMaskInfo.equals(""))) {
Log.e(TAG, "getDHCPMask: 动态子网掩码" + tempMaskInfo);
return tempMaskInfo;
} else {
Log.e(TAG, "getDHCPMask: 动态子网掩码" + mNullInfo);
return mNullInfo;
}
}
private String getDHCPIP() {
// 取得动态IP地址
String iface = mEthernetManager.getEthernetIfaceName();
String tempIpInfo = SystemProperties.get("dhcp." + iface + ".ipaddress");
if ((tempIpInfo != null) && (!tempIpInfo.equals(""))) {
Log.e(TAG, "getDHCPIP: 动态ip地址::" + tempIpInfo);
return tempIpInfo;
} else {
Log.e(TAG, "getDHCPIP: 动态ip地址::" + mNullInfo);
return mNullInfo;
}
}
private String getStaticIp() {
String mEthIpAddress = Settings1.System.getString(mContentResolver,
Settings1.System.ETHERNET_STATIC_IP);
Log.e(TAG, "getStaticIp: 静态ip地址为" + mEthIpAddress);
return mEthIpAddress;
}
public Net_State getNetState() {
// 是否使用静态IP
int useStaticIp = Settings1.System.getInt(mContentResolver,
Settings1.System.ETHERNET_USE_STATIC_IP, 0);
if (useStaticIp == 1) {
Log.e(TAG, "getNetState: 静态网络");
return Net_State.static_state;
} else {
Log.e(TAG, "getNetState: 动态网络");
return Net_State.dynamic_state;
}
}
private OnEthernetChangedCallback mCallback;
public void setCallback(OnEthernetChangedCallback callback) {
mCallback = callback;
}
public interface OnEthernetChangedCallback {
//连接中
void onConnecting();
//已连接
void onConnected(String ip, String mask, String gateway, String dns1, String dns2);
//断开中
void onDisconnecting();
//已断开
void onDisconnected(String ip, String mask, String gateway, String dns1, String dns2);
}
}