开发者

Can WIFI be turned off programmatically

开发者 https://www.devze.com 2023-02-28 02:30 出处:网络
How can wifi be turned off/开发者_如何转开发on programmatically and do is rooted or system app required for this.Permissions are required.

How can wifi be turned off/开发者_如何转开发on programmatically and do is rooted or system app required for this.


Permissions are required.

I just wrote this app that toggles Wifi.

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.stackoverflow.q5766518"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk
        android:minSdkVersion="3" />

    <uses-permission
        android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission
        android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission
        android:name="android.permission.WAKE_LOCK" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity
            android:name=".Main"
            android:label="@string/app_name">
            <intent-filter>
                <action
                    android:name="android.intent.action.MAIN" />
                <category
                    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/my_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toggle Wifi" />
</LinearLayout>

Main Activity

 @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button myButton = (Button) findViewById(R.id.my_button);
    myButton.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifi.setWifiEnabled(!wifi.isWifiEnabled());
      }
    });
  }


WIFI_ON is a secure setting; only apps signed by the system firmware will be able to hold the proper permission and adjust it using the SDK.


UPDATE

setWifiEnabled() probably supports this, as was pointed out in the comments. I don't see evidence of a permission being required, but if there is one, you'll get a stack trace that should point out what's needed. My apologies for forgetting about this path.


Yes, its possible. wifimangr.setWifiEnabled(false);

Create an object of Wifimanager..and call the method setWifiEnabled to "false". wifimangr.setWifiEnabled(false);

you need CHANGE_WIFI_STATE permission todo this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号