因项目需要监听网络状态实现LED闪烁功能,具体要求如下:
1.不管是WiFi还是以太网都需要监听。
2.网络未连接时亮红灯,网络连接成功但是不能成功上网时绿灯闪烁。
3.网络连接成功且可以上网时绿灯亮。
4.待机时无论网络可不可以连接,都要不亮。
为方便移植,通过编写开机启动的系统应用实现上述功能。
首先注册开机广播的方式启动网络监听服务:
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_BOOT_COMPLETED)) {
Intent netIntent = new Intent(Intent.ACTION_RUN);
netIntent.setClass(context, netStatusService.class);
netIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(netIntent);
}
}
然后编写网络监听服务,并在onCreate函数中注册广播
IntentFilter powerIntentFilter = new IntentFilter();
powerIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
powerIntentFilter.setPriority(PRIORITY_MIN);
registerReceiver(mPowerReceiver, powerIntentFilter);
IntentFilter screenOnFilter = new IntentFilter();
screenOnFilter.addAction(Intent.ACTION_SCREEN_ON);
screenOnFilter.setPriority(PRIORITY_MAX);
registerReceiver(mPowerReceiver, screenOnFilter);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(mNetStatusReceiver, intentFilter);
然后在mNetStatusReceiver中实现LED控制。
private final BroadcastReceiver mNetStatusReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (connectivityManager != null) {
connectivityManager.requestNetwork(new NetworkRequest.Builder().build(), new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
Log.d(LOG, "onAvailable");
}
@Override
public void onLost(Network network) {
super.onLost(network);
Log.e(LOG, "onLost");
mHwManager.GpioStatusSet(68, 0);
netStatus = 0;
mHandler.removeCallbacks(runnable);
}
@Override
public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
super.onLinkPropertiesChanged(network, linkProperties);
Log.d(LOG, "onLinkPropertiesChanged");
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities);
Log.d(LOG, "onCapabilitiesChanged");
NetworkInfo ethInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo activeInfo = connectivityManager.getActiveNetworkInfo();
LinkProperties ethLink = connectivityManager.getLinkProperties(ConnectivityManager.TYPE_ETHERNET);
LinkProperties wifiLink = connectivityManager.getLinkProperties(ConnectivityManager.TYPE_WIFI);
if((wifiInfo!=null) && (ethInfo!=null) && (activeInfo!=null)){
Log.d(LOG, "ethInfo:"+ethInfo.isConnected()+"\n"+"wifi:"+wifiInfo.isConnected()
+"\n"+"active:"+activeInfo.getTypeName()+"\n"+"networkCapabilities:"+networkCapabilities.toString()+"\n"+"ethLink:"+ethLink+"\n"+"wifiLink:"+wifiLink);
if(ethInfo.isConnected()){
if(ethLink != null)
{
if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED))
{
mHandler.removeCallbacks(runnable);
mHwManager.GpioStatusSet(68, 1);
netStatus = 1;
}else{
mHwManager.GpioStatusSet(68, 0);
netStatus = 0;
mHandler.removeCallbacks(runnable);
mHandler.post(runnable);
}
}
}else if(wifiInfo.isConnected()){
if(wifiLink != null){
if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED))
{
mHandler.removeCallbacks(runnable);
mHwManager.GpioStatusSet(68, 1);
netStatus = 1;
}else{
mHwManager.GpioStatusSet(68, 0);
netStatus = 0;
mHandler.removeCallbacks(runnable);
mHandler.post(runnable);
}
}
}else{
mHwManager.GpioStatusSet(68, 0);
netStatus = 0;
mHandler.removeCallbacks(runnable);
}
}else{
Log.d(LOG, "null ethInfo:"+ethInfo+"\n"+"wifi:"+wifiInfo+"\n"+"active:"+activeInfo);
mHwManager.GpioStatusSet(68, 0);
netStatus = 0;
mHandler.removeCallbacks(runnable);
}
}
@Override
public void onLosing(Network network, int maxMsToLive) {
super.onLosing(network, maxMsToLive);
Log.d(LOG, "onLosing");
}
@Override
public void onUnavailable() {
super.onUnavailable();
Log.d(LOGD, "onUnavailable");
}
});
}
}
};
但是其中灯控制接口GpioStatusSet需要根据不同平台自己实现进行替换。
代码链接如下。
https://download.csdn.net/download/xiaoyao19880622/11142269