Chinaunix首页 | 论坛 | 博客
  • 博客访问: 965729
  • 博文数量: 113
  • 博客积分: 7235
  • 博客等级: 少将
  • 技术积分: 2101
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-14 11:24
文章分类

全部博文(113)

文章存档

2013年(7)

2012年(5)

2011年(6)

2010年(8)

2009年(15)

2008年(72)

分类: Android平台

2013-06-28 15:57:39

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栏上的开关按钮:

  1. <!-- Network -->
  2.      <LinearLayout
  3.              android:id="@+id/network"
  4.              style="@style/StatusBarPanelSettingsRow"
  5.              >
  6.          <ImageView
  7.                  android:id="@+id/network_icon"
  8.                  style="@style/StatusBarPanelSettingsIcon"
  9.                  android:src="@drawable/ic_sysbar_wifi_on"
  10.                  />
  11.          <TextView
  12.                  android:id="@+id/network_label"
  13.                  style="@style/StatusBarPanelSettingsContents"
  14.                  android:text="@string/status_bar_settings_wifi_butt
  15.                  />
  16.          
  17.                  android:id="@+id/network_checkbox"
  18.                  android:layout_width="wrap_content"
  19.                  android:layout_height="wrap_content"
  20.                  android:layout_gravity="center_vertical"
  21.                  android:layout_marginRight="5dp
通过添加名为network_checkbox的开关按钮,就可以在wifi设置栏上显示一个开关了。接下来需要添加对此开关的操作代码,我们需要修改frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tablet/SettingsView.java

  1. import android.content.BroadcastReceiver;
  2. import android.net.wifi.WifiManager;
  3. import android.content.IntentFilter;
  4. public class SettingsView extends LinearLayout implements View.OnClickListener {
  5.     static final String TAG = "SettingsView";

  6.     AirplaneModeController mAirplane;
  7.     AutoRotateController mRotate;
  8.     BrightnessController mBrightness;
  9.     DoNotDisturbController mDoNotDisturb;
  10.     View mRotationLockContainer;
  11.     View mRotationLockSeparator;
  12.     private CompoundButton mNetworkCheckbox;
  13.     private BroadcastReceiver WifiStateReceiver = new BroadcastReceiver() {
  14.         @Override
  15.         public void onReceive(Context context, Intent intent) {
  16.             String action = intent.getAction();
  17.             if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
  18.                 WifiManager wifiManager =
  19.                     (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
  20.                 int wifiState = wifiManager.getWifiState();
  21.                 if (wifiState == wifiManager.WIFI_STATE_ENABLED)
  22.                     mNetworkCheckbox.setChecked(true);
  23.                 else if (wifiState == wifiManager.WIFI_STATE_DISABLED) {
  24.                     mNetworkCheckbox.setChecked(false);
  25.                 }
  26.             }
  27.         }
  28.     };
  29.     private CompoundButton.OnCheckedChangeListener NetworkCheckboxHandler =
  30.         new CompoundButton.OnCheckedChangeListener() {
  31.         public void onCheckedChanged(CompoundButton view, boolean checked) {
  32.             WifiManager wifiManager =
  33.                 (WifiManager)getContext().getSystemService(Context.WIFI_SERVICE);
  34.             int wifiState = wifiManager.getWifiState();
  35.             if (checked) {
  36.                 if (wifiState == wifiManager.WIFI_STATE_DISABLED) {
  37.                     Slog.d(TAG, "WIFI enable\n");
  38.                     wifiManager.setWifiEnabled(true);
  39.                 }
  40.             } else {
  41.                 if (wifiState == wifiManager.WIFI_STATE_ENABLED) {
  42.                     Slog.d(TAG, "WIFI disable\n");
  43.                     wifiManager.setWifiEnabled(false);
  44.                 }
  45.             }
  46.         }
  47.     };

  48.     public SettingsView(Context context, AttributeSet attrs) {
  49.         this(context, attrs, 0);
  50.     }

  51.     public SettingsView(Context context, AttributeSet attrs, int defStyle) {
  52.         super(context, attrs, defStyle);
  53.     }

  54.     @Override
  55.     protected void onFinishInflate() {
  56.         super.onFinishInflate();

  57.         final Context context = getContext();

  58.         mAirplane = new AirplaneModeController(context,
  59.                 (CompoundButton)findViewById(R.id.airplane_checkbox));

  60.         findViewById(R.id.network).setOnClickListener(this);
  61.         mNetworkCheckbox = (CompoundButton)findViewById(R.id.network_checkbox);
  62.         mNetworkCheckbox.setOnCheckedChangeListener(NetworkCheckboxHandler);

  63.         WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  64.         IntentFilter wifiIntentFilter = new IntentFilter();
  65.         wifiIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
  66.         context.registerReceiver(WifiStateReceiver, wifiIntentFilter);

  67.         mRotationLockContainer = findViewById(R.id.rotate);
  68.         mRotationLockSeparator = findViewById(R.id.rotate_separator);
  69.         mRotate = new AutoRotateController(context,
  70.                 (CompoundButton)findViewById(R.id.rotate_checkbox),
  71.                 new AutoRotateController.RotationLockCallbacks() {
  72.                     @Override
  73.                     public void setRotationLockControlVisibility(boolean show) {
  74.                         mRotationLockContainer.setVisibility(show ? View.VISIBLE : View.GONE);
  75.                         mRotationLockSeparator.setVisibility(show ? View.VISIBLE : View.GONE);
  76.                     }
  77.                 });

  78.         mBrightness = new BrightnessController(context,
  79.                 (ToggleSlider)findViewById(R.id.brightness));
  80.         mDoNotDisturb = new DoNotDisturbController(context,
  81.                 (CompoundButton)findViewById(R.id.do_not_disturb_checkbox));
  82.         findViewById(R.id.settings).setOnClickListener(this);
  83.     }
这里说明一下,主要改动是在onFinishInflate函数中获取到开关控件network_checkbox,而后为其添加setOnCheckedChangeListener以处理用户的点击事件,在点击回调里,通过wifiManager.setWifiEnabled函数打开和关闭wifi。而注册一个WifiStateReceiver主要是用来同步wifi的开关状态的,因为如果用户在Settings里面打开了wifi,那相应的状态栏中的wifi开关状态也应该跟着同步变化。
修改后效果如下:

阅读(8441) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~