摘自: 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 /> <switch< span="" style="word-wrap: break-word;"> 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.javaimport 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 enablen"); wifiManager.setWifiEnabled(true); } } else { if (wifiState == wifiManager.WIFI_STATE_ENABLED) { Slog.d(TAG, "WIFI disablen"); 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开关状态也应该跟着同步变化。修改后效果如下:
推荐整理分享Android 4.1.2为通知栏添加wifi开关(安卓hid通信),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:安卓通用,安卓通道,android 通信,android系统提供了什么通信,android 通信,android 通信,android 通信,android通信协议,内容如对您有帮助,希望把文章链接给更多的朋友!
Android 4.4 eng版本 红框问题 在android4.4.2中编译的eng版本经常出现红框的问题。通过排查因为在frameworks/base/core/java/android/os/StrictMode.java文件中打开了设置选项具体修改如下:---a/frame
Activity与Fragment数据传递之Fragment从Activity获取数据 整理Fragment与Activity之间的数据交换,大体上包括三种:1、Fragment从Activity获取数据2、Activity从Fragment获取数据3、Fragment之间获取数据通常,如果Activity向
使用android.graphics.Path类自绘制PopupWindow背景 PopupWindow简介PopupWindow是悬浮在当前activity上的一个容器,用它可以展示任意的内容。PopupWindow跟位置有关的API有下面几个:showAsDropDown(Viewanchor,intxoff,intyo