很多时候我们需要对当前手机的网络状态进行判断。
例如:
没有网络时打开应用,应该加载缓存数据,不去请求数据等。
当前为wifi网络时,应该加载高清图片,视频自动下载缓存等。
当前为2G网络时,应该停止下载等高流量需求的操作,并提示用户等。
.....
.....
如何监听网络的变化呢?用后台服务是一个很好的选择。既能实时获取当前Android
的网络状态,还能不依赖与Activity
的生命周期。
一.编写服务类
代码注释已经很详细了,主要步骤就是自定义广播实例,并注册系统网络连接的广播(IntentFilter).
public class NetworkStateService extends Service {
// Class that answers queries about the state of network connectivity.
// 系统网络连接相关的操作管理类.
private ConnectivityManager connectivityManager;
// Describes the status of a network interface.
// 网络状态信息的实例
private NetworkInfo info;
/**
* 当前处于的网络
* 0 :null
* 1 :2G/3G
* 2 :wifi
*/
public static int networkStatus;
public static final String NETWORKSTATE = "com.text.android.network.state"; // An action name
/**
* 广播实例
*/
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// The action of this intent or null if none is specified.
// action是行动的意思,也许是我水平问题无法理解为什么叫行动,我一直理解为标识(现在理解为意图)
String action = intent.getAction(); //当前接受到的广播的标识(行动/意图)
// 当当前接受到的广播的标识(意图)为网络状态的标识时做相应判断
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
// 获取网络连接管理器
connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// 获取当前网络状态信息
info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
//当NetworkInfo不为空且是可用的情况下,获取当前网络的Type状态
//根据NetworkInfo.getTypeName()判断当前网络
String name = info.getTypeName();
//更改NetworkStateService的静态变量,之后只要在Activity中进行判断就好了
if (name.equals("WIFI")) {
networkStatus = 2;
} else {
networkStatus = 1;
}
} else {
// NetworkInfo为空或者是不可用的情况下
networkStatus = 0;
Toast.makeText(context, "没有可用网络!\n请连接网络后刷新本界面", Toast.LENGTH_SHORT).show();
Intent it = new Intent();
it.putExtra("networkStatus", networkStatus);
it.setAction(NETWORKSTATE);
sendBroadcast(it); //发送无网络广播给注册了当前服务广播的Activity
/**
* 这里推荐使用本地广播的方式发送:
* LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
*/
}
}
}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//注册网络状态的广播,绑定到mReceiver
IntentFilter mFilter = new IntentFilter();
mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mReceiver, mFilter);
}
@Override
public void onDestroy() {
super.onDestroy();
//注销接收
unregisterReceiver(mReceiver);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
/**
* 判断网络是否可用
*/
public static boolean isNetworkAvailable(Context context) {
// 获取网络连接管理器
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// 获取当前网络状态信息
NetworkInfo[] info = mgr.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
}
二.启动服务
在Activity中启动服务
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qzmobile_main);
Intent intent = new Intent();
intent.setAction("com.text.service.NetworkStateService");
startService(intent);
}
三.注册服务
服务是四大组件之一,当然不能忘记在AndroidManifest.xml
中注册服务。
<service android:name="com.text.service.NetworkStateService" >
<intent-filter android:priority="1000" >
<action android:name="com.text.service.NetworkStateService" />
</intent-filter>
</service>
最后不要忘了网络权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
至此,你就能通过自己定义的服务实施监控网络状态了,当网络状态转变的时候,你也对Activity
发出相应要处理的广播即可。
在播放器中,我们常常可以看到这么一个设计,就是用户通过在屏幕的某个部分上下滑动就可以调节屏幕的亮度,在某个部分上下滑动就可以调节播放的音量。而左右滑动就可以调节播放的进度。
今天,我要说一下亮度的调节。其实主要是通过设置View的属性实现的。
if (params.screenBrightness <= 0.01f) { params.screenBrightness = 0.01f; } window.setAttributes(params); mCurrentDeltaBrintness = 0; } performLightChange(params.screenBrightness); }
上述代码就是在左边部分上下滑动的时候会被调用。
另附performLigthChange的代码。
我们在举一个相对好切入的例子。