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();    }}

每天记录一点,每天进步一点

更多相关文章

  1. Android(安卓)Retrofit 2.0自定义JSONObject Converter
  2. Android(安卓)9.0模拟器root权限获取
  3. Android(安卓)MVVM之Room学习记录,从零开始实现
  4. Android(安卓)监听Home键
  5. 【Android应用开发】Android(安卓)蓝牙低功耗 (BLE) ( 第一篇 .
  6. Android(安卓)App耗电量统计
  7. Android(安卓)动态申请存储权限
  8. 用Android自带的signapk.jar + .x509.pem + .pk8签名应用程序
  9. binder机制底层驱动浅析

随机推荐

  1. android仿腾讯安全管家首页抽屉效果
  2. android版PDA通过USB与.net应用程序通讯,
  3. Android音量调节的实现(RingtoneManager
  4. Android自学笔记-17-广播接收者(Broadcast
  5. 实践中探索Android智能手机系统------APK
  6. Android的死机、重启问题分析方法
  7. Android(安卓)UI 优化 [ 类别:Layout ] #3
  8. 基于Android与云平台的多功能社交软件(毕
  9. Android仿“守望先锋”加载动画
  10. Android(安卓)Studio中提交library到Jcen