需求:实现移动数据打开和关闭功能 代码路径: frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java 1、初始化网络和移动数据控制
import com.android.systemui.statusbar.policy.NetworkController;
import com.android.settingslib.net.DataUsageController;
private NetworkController mNTController;
private DataUsageController mDataUsageController;
private void init(Context context) {
this.mContext = context;
mNTController = Dependency.get(NetworkController.class);
mDataUsageController = mNTController.getMobileDataController();
}
2、控制移动数据打开和关闭接口
public void setMobileDataEnable(boolean enabled) {
if (null != mDataUsageController) {
mDataUsageController.setMobileDataEnabled(enabled);
}
}
3、获取当前移动数据是否打开
private boolean isMobileDataEnable() {
if (null != mDataUsageController) {
return mDataUsageController.isMobileDataEnabled();
}
return false;
}
4、当前是否支持移动数据功能
private boolean isMobileDataSupported() {
if (null != mDataUsageController) {
return mDataUsageController.isMobileDataSupported();
}
return false;
}
5、如何监听移动数据打开关闭状态,需要实现DataUsageController.NetworkNameProvider接口并且实现里面getMobileDataNetworkName方法
public class MyContainer extends LinearLayout implements View.OnClickListener, DataUsageController.NetworkNameProvider{
mDataUsageController.setNetworkController(this);
mDataUsageController.setCallback(new DataUsageController.Callback() {
@Override
public void onMobileDataEnabled(boolean enabled) {
Log.i(TAG, "onMobileDataEnabled() enabled = " + enabled);
mHandler.post(new Runnable() {
@Override
public void run() {
updateDataStatus(enabled);
}
});
}
});
@Override
public String getMobileDataNetworkName() {
return "";
}
}
|