Android热点、WiFi、蓝牙等功能开启和关闭
16lz
2021-01-25
Android热点、WiFi、蓝牙等功能开启和关闭
1、需要添加权限,选择自己需要的添加,热点的开启和关闭可能还需要权限;
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(); }}
每天记录一点,每天进步一点
更多相关文章
- Android(安卓)Retrofit 2.0自定义JSONObject Converter
- Android(安卓)9.0模拟器root权限获取
- Android(安卓)MVVM之Room学习记录,从零开始实现
- Android(安卓)监听Home键
- 【Android应用开发】Android(安卓)蓝牙低功耗 (BLE) ( 第一篇 .
- Android(安卓)App耗电量统计
- Android(安卓)动态申请存储权限
- 用Android自带的signapk.jar + .x509.pem + .pk8签名应用程序
- binder机制底层驱动浅析