«

Android 4.1.2为通知栏添加wifi开关

时间:2024-3-2 19:29     作者:韩俊     分类: Android


摘自:http://blog.chinaunix.net/uid-20729605-id-3779842.html

在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 -->

 &lt;LinearLayout<br style="word-wrap:break-word">

         android:id=&quot;@&#43;id/network&quot;<br style="word-wrap:break-word">

         style=&quot;@style/StatusBarPanelSettingsRow&quot;<br style="word-wrap:break-word">

         &gt;<br style="word-wrap:break-word">

     &lt;ImageView<br style="word-wrap:break-word">

             android:id=&quot;@&#43;id/network_icon&quot;<br style="word-wrap:break-word">

             style=&quot;@style/StatusBarPanelSettingsIcon&quot;<br style="word-wrap:break-word">

             android:src=&quot;@drawable/ic_sysbar_wifi_on&quot;<br style="word-wrap:break-word">

             /&gt;<br style="word-wrap:break-word">

     &lt;TextView<br style="word-wrap:break-word">

             android:id=&quot;@&#43;id/network_label&quot;<br style="word-wrap:break-word">

             style=&quot;@style/StatusBarPanelSettingsContents&quot;<br style="word-wrap:break-word">

             android:text=&quot;@string/status_bar_settings_wifi_butt<br style="word-wrap:break-word">

             /&gt;<br style="word-wrap:break-word">

     &lt;switch&lt; span=&quot;&quot; style=&quot;word-wrap: break-word;&quot;&gt;<br style="word-wrap:break-word">

             android:id=&quot;@&#43;id/network_checkbox&quot; <br style="word-wrap:break-word">

             android:layout_width=&quot;wrap_content&quot;<br style="word-wrap:break-word">

             android:layout_height=&quot;wrap_content&quot;<br style="word-wrap:break-word">

             android:layout_gravity=&quot;center_vertical&quot;<br style="word-wrap:break-word">

             android:layout_marginRight=&quot;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 = &quot;SettingsView&quot;;<br style="word-wrap:break-word">


AirplaneModeController mAirplane;<br style="word-wrap:break-word">

AutoRotateController mRotate;<br style="word-wrap:break-word">

BrightnessController mBrightness;<br style="word-wrap:break-word">

DoNotDisturbController mDoNotDisturb;<br style="word-wrap:break-word">

View mRotationLockContainer;<br style="word-wrap:break-word">

View mRotationLockSeparator;<br style="word-wrap:break-word">

private CompoundButton mNetworkCheckbox;<br style="word-wrap:break-word">

private BroadcastReceiver WifiStateReceiver = new BroadcastReceiver() {<br style="word-wrap:break-word">

    @Override<br style="word-wrap:break-word">

    public void onReceive(Context context, Intent

intent) {

        String action = intent.getAction();<br style="word-wrap:break-word">

        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {<br style="word-wrap:break-word">

            WifiManager wifiManager = <br style="word-wrap:break-word">

                (WifiManager)context.getSystemService(Context.WIFI_SERVICE);<br style="word-wrap:break-word">

            int wifiState = wifiManager.getWifiState();<br style="word-wrap:break-word">

            if (wifiState == wifiManager.WIFI_STATE_ENABLED)<br style="word-wrap:break-word">

                mNetworkCheckbox.setChecked(true);<br style="word-wrap:break-word">

            else if (wifiState == wifiManager.WIFI_STATE_DISABLED) {<br style="word-wrap:break-word">

                mNetworkCheckbox.setChecked(false);<br style="word-wrap:break-word">

            }<br style="word-wrap:break-word">

        }<br style="word-wrap:break-word">

    }<br style="word-wrap:break-word">

};<br style="word-wrap:break-word">

private CompoundButton.OnCheckedChangeListener

NetworkCheckboxHandler =

    new CompoundButton.OnCheckedChangeListener() {<br style="word-wrap:break-word">

    public void onCheckedChanged(CompoundButton view, boolean checked) {<br style="word-wrap:break-word">

        WifiManager wifiManager = <br style="word-wrap:break-word">

            (WifiManager)getContext().getSystemService(Context.WIFI_SERVICE);<br style="word-wrap:break-word">

        int wifiState = wifiManager.getWifiState();<br style="word-wrap:break-word">

        if (checked) {<br style="word-wrap:break-word">

            if (wifiState == wifiManager.WIFI_STATE_DISABLED) {<br style="word-wrap:break-word">

                Slog.d(TAG, &quot;WIFI

enablen");

                wifiManager.setWifiEnabled(true);<br style="word-wrap:break-word">

            }<br style="word-wrap:break-word">

        } else {<br style="word-wrap:break-word">

            if (wifiState == wifiManager.WIFI_STATE_ENABLED) {<br style="word-wrap:break-word">

                Slog.d(TAG, &quot;WIFI

disablen");

                wifiManager.setWifiEnabled(false);<br style="word-wrap:break-word">

            }<br style="word-wrap:break-word">

        }<br style="word-wrap:break-word">

    }<br style="word-wrap:break-word">

};<br style="word-wrap:break-word">


public SettingsView(Context context, AttributeSet attrs) {<br style="word-wrap:break-word">

    this(context, attrs, 0);<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">


public SettingsView(Context context, AttributeSet attrs, int defStyle) {<br style="word-wrap:break-word">

    super(context, attrs, defStyle);<br style="word-wrap:break-word">

}<br style="word-wrap:break-word">


@Override<br style="word-wrap:break-word">

protected void onFinishInflate() {<br style="word-wrap:break-word">

    super.onFinishInflate();<br style="word-wrap:break-word">


    final Context context = getContext();<br style="word-wrap:break-word">


    mAirplane = new AirplaneModeController(context,<br style="word-wrap:break-word">

            (CompoundButton)findViewById(R.id.airplane_checkbox));<br style="word-wrap:break-word">


    findViewById(R.id.network).setOnClickListener(this);<br style="word-wrap:break-word">

    mNetworkCheckbox = (CompoundButton)findViewById(R.id.network_checkbox);<br style="word-wrap:break-word">

    mNetworkCheckbox.setOnCheckedChangeListener(NetworkCheckboxHandler);<br style="word-wrap:break-word">


    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);<br style="word-wrap:break-word">

    IntentFilter wifiIntentFilter = new IntentFilter();<br style="word-wrap:break-word">

    wifiIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);<br style="word-wrap:break-word">

    context.registerReceiver(WifiStateReceiver, wifiIntentFilter);<br style="word-wrap:break-word">


    mRotationLockContainer = findViewById(R.id.rotate);<br style="word-wrap:break-word">

    mRotationLockSeparator = findViewById(R.id.rotate_separator);<br style="word-wrap:break-word">

    mRotate = new AutoRotateController(context,<br style="word-wrap:break-word">

            (CompoundButton)findViewById(R.id.rotate_checkbox),<br style="word-wrap:break-word">

            new AutoRotateController.RotationLockCallbacks() {<br style="word-wrap:break-word">

                @Override<br style="word-wrap:break-word">

                public void setRotationLockControlVisibility(boolean show) {<br style="word-wrap:break-word">

                    mRotationLockContainer.setVisibility(show ? View.VISIBLE : View.GONE);<br style="word-wrap:break-word">

                    mRotationLockSeparator.setVisibility(show ? View.VISIBLE : View.GONE);<br style="word-wrap:break-word">

                }<br style="word-wrap:break-word">

            });<br style="word-wrap:break-word">


    mBrightness = new BrightnessController(context,<br style="word-wrap:break-word">

            (ToggleSlider)findViewById(R.id.brightness));<br style="word-wrap:break-word">

    mDoNotDisturb = new DoNotDisturbController(context,<br style="word-wrap:break-word">

            (CompoundButton)findViewById(R.id.do_not_disturb_checkbox));<br style="word-wrap:break-word">

    findViewById(R.id.settings).setOnClickListener(this);<br style="word-wrap:break-word">

}

这里说明一下,主要改动是在onFinishInflate函数中获取到开关控件network_checkbox,而后为其添加setOnCheckedChangeListener以处理用户的点击事件,在点击回调里,通过wifiManager.setWifiEnabled函数打开和关闭wifi。而注册一个WifiStateReceiver主要是用来同步wifi的开关状态的,因为如果用户在Settings里面打开了wifi,那相应的状态栏中的wifi开关状态也应该跟着同步变化。

修改后效果如下:

标签: android

热门推荐