赞
踩
1、需要添加权限,选择自己需要的添加,热点的开启和关闭可能还需要权限;
<!--设备(亮屏)权限-->
<user-permission android:name="android.permission.DEVICE_POWER" />
<!--息屏权限-->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!--蓝牙权限-->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- wifi和热点权限 -->
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<!-- 网络权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORKE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
2、下面是操作代码工具类:
import android.annotation.SuppressLint;
import android.app.admin.DevicePolicyManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.PowerManager;
import android.util.Log;
import com.example.txzvoicedemo.SDKDemoApp;
import java.lang.reflect.Method;
/**
* @time 2020年4月9日10:54:03
* @author fenghaitao
* 系统功能操作类(WiFi、热点、屏幕等开关操作)
*/
public class SwitchUtils {
private static final String TAG = "SwitchUtils类";
/**
* 热点开关
*/
public static boolean apSwitch(Context context, boolean flag){
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
}
try{
WifiConfiguration apConfig = new WifiConfiguration();
apConfig.SSID ="ap"; //配置热点的名称
apConfig.preSharedKey = "12345678"; //配置热点的密码(至少8位)
apConfig.allowedKeyManagement.set(4); //配置密码加密方式
//通过反射调用设置热点
Method method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
Boolean rs = (Boolean) method.invoke(wifiManager, apConfig, flag);//true开启热点 false关闭热点
Log.d(TAG, "开启是否成功:" + rs);
return rs;
} catch(Exception e){
e.printStackTrace();
return false;
}
}
/**
* WiFi开关
*/
public static void wifiSwitch(Context context, boolean flag){
//获取wifi管理服务
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(flag);
}
/**
* 蓝牙开关
*/
public static void blueSwitch(boolean flag){
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (flag) {
bluetoothAdapter.enable();
}else{
bluetoothAdapter.disable();
}
}
/**
* 熄屏
*/
public static void ScreenOff(Context context) {
DevicePolicyManager policyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
policyManager.lockNow();
}
/**
* 亮屏
*/
public static void ScreenOn(Context context){
//获取电源管理器对象
PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
@SuppressLint("InvalidWakeLockTag")
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");
//点亮屏幕
wl.acquire();
//释放
wl.release();
}
}
每天记录一点,每天进步一点
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。