首页 > 解决方案 > Wifi状态不会在android中改变?

问题描述

大家好,每个人都在使用 android Q 设备,我有一个小问题是 wifi 状态不会改变这是我的代码

   

     public class wise extends AppCompatActivity {
    ActivityMainBinding binding;
  private WifiManager wifimanager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding=ActivityMainBinding.inflate(getLayoutInflater());
        View view=binding.getRoot();
        setContentView(view);
    wifimanager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    binding.Switchbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                wifimanager.setWifiEnabled(true);
                binding.Switchbtn.setText("wifi is on ");
            }else {
                wifimanager.setWifiEnabled(false);
                binding.Switchbtn.setText("wifi is off ");
            }
            Toast.makeText(getApplicationContext(),String.valueOf(wifimanager.isWifiEnabled()),Toast.LENGTH_SHORT).show();
        }
    });
     if(wifimanager.isWifiEnabled()){
         binding.Switchbtn.setChecked(true);
         binding.Switchbtn.setText("wifi_is on");
     }else{

         binding.Switchbtn.setChecked(false);
         binding.Switchbtn.setText("wifi_is off");
     }
}}

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hello_world">

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

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity android:name="com.example.hello_world.wise">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我用来自youtube的流的代码编写代码是否有任何syggestions请帮助stackoverflow询问帖子的更多详细信息我没有因为这是一个简单的问题并且不需要太多细节所以我写这个填补职位对不起

标签: androidandroid-studioandroid-wifiwifimanager

解决方案


推荐阅读