在Android 4.1.2系统中,默认的通知栏上有个WIFI的选项,点击该选项系统会弹出settings应用的wifi设置页面,而后我们进行wifi的连接,并且可以正常上网。理论上在之后的使用中我们可以一直打开着wifi,而不用再次进入wifi设置页面,但偶尔还是需要关闭wifi,比如为了省电。此时当我们要关闭的时候,还是需要进入wifi设置页面。所以为了方便期间,可以通过修改SystemUI的通知栏,zaiwifi一栏添加一个wifi的开关,这样就可以很方便的打开和关闭wifi了。
为实现此功能,我们需要修改两个地方,首先需要在frameworks/base/packages/SystemUI/res/layout/system_bar_settings_view.xml中添加wifi栏上的开关按钮:
-
<!-- Network -->
-
<LinearLayout
-
android:id="@+id/network"
-
style="@style/StatusBarPanelSettingsRow"
-
>
-
<ImageView
-
android:id="@+id/network_icon"
-
style="@style/StatusBarPanelSettingsIcon"
-
android:src="@drawable/ic_sysbar_wifi_on"
-
/>
-
<TextView
-
android:id="@+id/network_label"
-
style="@style/StatusBarPanelSettingsContents"
-
android:text="@string/status_bar_settings_wifi_butt
-
/>
-
-
android:id="@+id/network_checkbox"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_gravity="center_vertical"
-
android:layout_marginRight="5dp
通过添加名为network_checkbox的开关按钮,就可以在wifi设置栏上显示一个开关了。接下来需要添加对此开关的操作代码,我们需要修改frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tablet/SettingsView.java
-
import android.content.BroadcastReceiver;
-
import android.net.wifi.WifiManager;
-
import android.content.IntentFilter;
-
public class SettingsView extends LinearLayout implements View.OnClickListener {
-
static final String TAG = "SettingsView";
-
-
AirplaneModeController mAirplane;
-
AutoRotateController mRotate;
-
BrightnessController mBrightness;
-
DoNotDisturbController mDoNotDisturb;
-
View mRotationLockContainer;
-
View mRotationLockSeparator;
-
private CompoundButton mNetworkCheckbox;
-
private BroadcastReceiver WifiStateReceiver = new BroadcastReceiver() {
-
@Override
-
public void onReceive(Context context, Intent intent) {
-
String action = intent.getAction();
-
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
-
WifiManager wifiManager =
-
(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
-
int wifiState = wifiManager.getWifiState();
-
if (wifiState == wifiManager.WIFI_STATE_ENABLED)
-
mNetworkCheckbox.setChecked(true);
-
else if (wifiState == wifiManager.WIFI_STATE_DISABLED) {
-
mNetworkCheckbox.setChecked(false);
-
}
-
}
-
}
-
};
-
private CompoundButton.OnCheckedChangeListener NetworkCheckboxHandler =
-
new CompoundButton.OnCheckedChangeListener() {
-
public void onCheckedChanged(CompoundButton view, boolean checked) {
-
WifiManager wifiManager =
-
(WifiManager)getContext().getSystemService(Context.WIFI_SERVICE);
-
int wifiState = wifiManager.getWifiState();
-
if (checked) {
-
if (wifiState == wifiManager.WIFI_STATE_DISABLED) {
-
Slog.d(TAG, "WIFI enable\n");
-
wifiManager.setWifiEnabled(true);
-
}
-
} else {
-
if (wifiState == wifiManager.WIFI_STATE_ENABLED) {
-
Slog.d(TAG, "WIFI disable\n");
-
wifiManager.setWifiEnabled(false);
-
}
-
}
-
}
-
};
-
-
public SettingsView(Context context, AttributeSet attrs) {
-
this(context, attrs, 0);
-
}
-
-
public SettingsView(Context context, AttributeSet attrs, int defStyle) {
-
super(context, attrs, defStyle);
-
}
-
-
@Override
-
protected void onFinishInflate() {
-
super.onFinishInflate();
-
-
final Context context = getContext();
-
-
mAirplane = new AirplaneModeController(context,
-
(CompoundButton)findViewById(R.id.airplane_checkbox));
-
-
findViewById(R.id.network).setOnClickListener(this);
-
mNetworkCheckbox = (CompoundButton)findViewById(R.id.network_checkbox);
-
mNetworkCheckbox.setOnCheckedChangeListener(NetworkCheckboxHandler);
-
-
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
-
IntentFilter wifiIntentFilter = new IntentFilter();
-
wifiIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
-
context.registerReceiver(WifiStateReceiver, wifiIntentFilter);
-
-
mRotationLockContainer = findViewById(R.id.rotate);
-
mRotationLockSeparator = findViewById(R.id.rotate_separator);
-
mRotate = new AutoRotateController(context,
-
(CompoundButton)findViewById(R.id.rotate_checkbox),
-
new AutoRotateController.RotationLockCallbacks() {
-
@Override
-
public void setRotationLockControlVisibility(boolean show) {
-
mRotationLockContainer.setVisibility(show ? View.VISIBLE : View.GONE);
-
mRotationLockSeparator.setVisibility(show ? View.VISIBLE : View.GONE);
-
}
-
});
-
-
mBrightness = new BrightnessController(context,
-
(ToggleSlider)findViewById(R.id.brightness));
-
mDoNotDisturb = new DoNotDisturbController(context,
-
(CompoundButton)findViewById(R.id.do_not_disturb_checkbox));
-
findViewById(R.id.settings).setOnClickListener(this);
-
}
这里说明一下,主要改动是在onFinishInflate函数中获取到开关控件network_checkbox,而后为其添加setOnCheckedChangeListener以处理用户的点击事件,在点击回调里,通过wifiManager.setWifiEnabled函数打开和关闭wifi。而注册一个WifiStateReceiver主要是用来同步wifi的开关状态的,因为如果用户在Settings里面打开了wifi,那相应的状态栏中的wifi开关状态也应该跟着同步变化。
修改后效果如下:
阅读(8450) | 评论(0) | 转发(1) |