android - 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询问帖子的更多详细信息我没有因为这是一个简单的问题并且不需要太多细节所以我写这个填补职位对不起
解决方案
推荐阅读
- jquery - UI Sortable - 我怎样才能让一些 div 在一个 sortable 中保持完整并且在其他 sortables 改变时永远不会移动或改变
- xamarin - Xamarin UWP 项目中的 CrossGeolocator 返回错误的当前位置
- android - 我们应该为电子商务 Android 应用程序使用后端代码还是客户端代码
- java - 准备好的语句中的 IBM DB2 标量函数参数
- python - 从目录中找到最新的文件夹并重命名
- r - 可视化 3D GMM
- javascript - 2小时之间的时差
- rust - 如何从 nalgebra 板条箱中获取 (1.0, 1.0, 1.0) Vector3?
- python - 有没有办法使用多核进行多线程处理?
- flutter - 如何在颤动中根据日期和时间对地图列表进行排序