網上移植ethernet框架的文章很多,這裡就不多說了,主要說下如何修改默認狀態。如下就簡單的說下,我也還沒有研究的很清楚。
首先服務的建在:
frameworks/base/services/java/com/Android/server/ConnectivityService.java
有如下代碼:
EthernetStateTracker est = new EthernetStateTracker(context, mHandler);
EthernetService ethService = new EthernetService(context, est);
ServiceManager.addService(Context.ETHERNET_SERVICE, ethService);
mNetTrackers[ConnectivityManager.TYPE_ETHERNET] = est;
est.startMonitoring();
這個應該是最開始執行的時候運行的。也就是說服務建後執行了這個est.startMonitoring();
這個函數在:
frameworks/base/ethernet/java/android/net/ethernet/EthernetStateTracker.java
代碼如下:
@Override
public void startMonitoring() {
if (localLOGV) Slog.v(TAG,"start to monitor the ethernet devices");
if (mServiceStarted) {
mEM = (EthernetManager)mContext.getSystemService(Context.ETHERNET_SERVICE);
int state = mEM.getState();
if (state != mEM.ETHERNET_STATE_DISABLED) {
if (state == mEM.ETHERNET_STATE_UNKNOWN) {
// maybe this is the first time we run, so set it to enabled
mEm.setEnabled(true);
} else {
try {
resetInterface();
} catch (UnknownHostException e) {
Slog.e(TAG, "Wrong ethernet configuration");
}
}
}
}
}
@Override
public void startMonitoring() {
if (localLOGV) Slog.v(TAG,"start to monitor the ethernet devices");
if (mServiceStarted) {
mEM = (EthernetManager)mContext.getSystemService(Context.ETHERNET_SERVICE);
int state = mEM.getState();
if (state != mEM.ETHERNET_STATE_DISABLED) {
if (state == mEM.ETHERNET_STATE_UNKNOWN) {
// maybe this is the first time we run, so set it to enabled
mEm.setEnabled(true);
} else {
try {
resetInterface();
} catch (UnknownHostException e) {
Slog.e(TAG, "Wrong ethernet configuration");
}
}
}
}
}
其中state=mEm.getState();
調用的是EthernetManager裡面的getState這個函數在:
frameworks/base/ethernet/java/android/net/ethernet/EthernetManager.java
代碼如下:
/**
* Get ethernet service state
* @return the state of the ethernet service
*/
public int getState( ) {
try {
return mService.getState();
} catch (RemoteException e) {
return 0;
}
}
而這裡又調用的Service裡面的getState函數在:
frameworks/base/services/java/com/android/server/EthernetService.java
代碼如下:
/**
* Get ethernet service state
* @return the state of the ethernet service
*/
public int getState( ) {
return mEthState;
}