I don't know where the problem is, as Google is answering it already:
If apps are targeting an older SDK ( Build.VERSION_CODES.P or below), they can continue to use this API.
So change your Target SDK to 28 and it works fine on Android Q.
Or if you need to change WiFi State by second Apps like Tasker or Automate:
- Install Android Studio
- Create a new Project namend WiFiOn with Empty Activity and SDK 28
- Add commented lines:
import androidx.appcompat.app.AppCompatActivity;
import android.net.wifi.WifiManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Add WiFi On Part
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true); // true or false to activate/deactivate wifi
// Add Toast if you want to
Toast toast = Toast.makeText(getApplicationContext(), "WiFi on", Toast.LENGTH_SHORT);
toast.show();
// Add Close Activity immediatelly
finish();
}
}
- Change minSdkVersion and targetSdkVersion to 28 in
build.grade(:app)
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.stackoverflow.example"
minSdkVersion 28
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
- Add Permission to AndroidMAnifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.p1apps.wifion">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<application
android:allowBackup="true"
...
Use Android Studio to install it on your phone.
Create a new Project like in 3. and name it WiFiOff and repeat all steps with changed lines in MainActivity:
...
wifi.setWifiEnabled(false); // true or false to activate/deactivate wifi
...
Toast toast = Toast.makeText(getApplicationContext(), "WiFi off",
...