最近项目需要一个开启WiFi热点的功能,由于Android 7.1实现机制跟之前版本有所不同,在网上搜索了很久终于解决了这个问题,特意把过程记录下来,希望大家不要踩坑。

开启WiFi热点函数  主要参考博客 : https://blog.csdn.net/zhang01/article/details/79222057

可以用一个按钮来触发开启热点。

public boolean setWifiApEnabled(boolean enabled) {        if (enabled) { // disable WiFi in any case            wifiManager.setWifiEnabled(false);        }               wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);        mContext = this;        handler = new Handler(getMainLooper());        try {            mResultReceiver = new ResultReceiver(handler);            WifiConfiguration apConfig = new WifiConfiguration();            apConfig.SSID = "asdf";            apConfig.preSharedKey = "12121212";            apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {                             Method mMethod = wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);                mMethod.invoke(wifiManager,apConfig);                              ConnectivityManager connectivityManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);                              Field mField = connectivityManager.getClass().getDeclaredField("TETHERING_WIFI");                mField.setAccessible(true);                WriteToLog.writeToLog("field.setAccessible ");                int mTETHERING_WIFI = (int)mField.get(connectivityManager);                Log.v("mTETHERING_WIFI:",String.valueOf(mTETHERING_WIFI));                       Field iConnMgrField = connectivityManager.getClass().getDeclaredField("mService");                iConnMgrField.setAccessible(true);                Object iConnMgr = iConnMgrField.get(connectivityManager);                Class<?> iConnMgrClass = Class.forName(iConnMgr.getClass().getName());                             Method mStartTethering1 = iConnMgrClass.getMethod("startTethering", int.class,ResultReceiver.class,boolean.class);                mStartTethering1.setAccessible(true);                mStartTethering1.invoke(iConnMgr, mTETHERING_WIFI,new ResultReceiver(handler){                    @Override                    protected void onReceiveResult(int resultCode, Bundle resultData) {                    super.onReceiveResult(resultCode, resultData);                    }                },true);                return true;            } else {                Method method = wifiManager.getClass().getMethod(                        "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);                return (Boolean) method.invoke(wifiManager, apConfig, enabled);            }        } catch (NoSuchMethodException e) {            e.printStackTrace();            WriteToLog.writeToLog("setWifiApEnabled Exception1: " + e.getMessage());            return false;        } catch (InvocationTargetException e) {            e.printStackTrace();            WriteToLog.writeToLog("setWifiApEnabled Exception2: " + e.getMessage());            return false;        } catch (IllegalAccessException e) {            e.printStackTrace();            WriteToLog.writeToLog("setWifiApEnabled Exception3: " + e.getMessage());            return false;        } catch (ClassNotFoundException e) {            e.printStackTrace();            WriteToLog.writeToLog("setWifiApEnabled Exception4: " + e.getMessage());            return false;        } catch (NoSuchFieldException e) {            e.printStackTrace();            WriteToLog.writeToLog("setWifiApEnabled Exception5: " + e.getMessage());            return false;        }    }

但是运行这个代码以后会抛出异常InvocationTargetException :null 

需要在AndroidManifest.xml文件中增添:android:sharedUserId="andorid.uid.system"

打包出apk,使用 signapk.jar 给自己的apk签名。

java -jar signapk.jar platform.x509.pem platform.pk8 src.apk  dst.apk

将签名好的apk 使用adb push 到手机的/system/app下,注意要使用chmod更改权限

reboot 重启手机  此时你就会发现你的apk的user变成了system这个时候就可以成功开启热点了。

更多相关文章

  1. 认识Android手机--来自MIUI
  2. Android Studio如何使用快捷键生成get,set,tostring,构造函数
  3. Android 系统蓝牙 控制手机端音乐暂停 (AVRCP)
  4. Android 4.0 虚拟按键、手机模式、平板模式
  5. eclipse 调试手机Android
  6. 说说 Android 中如何实现同时兼容手机与平板的新闻应用界面
  7. Android手机拨打电话、手动发送短信与自动拨打电话、自动发送短

随机推荐

  1. Android(安卓)N 指纹框架
  2. Android(安卓)UI系列 - 布局 - 目录
  3. android 系统中静音后使得音量减键不能解
  4. 【30篇突击 android】源码统计四
  5. SlidingMenu和ActionBarSherlock结合做出
  6. android 布局式跑马灯,非TextView
  7. Android如何获得系统版本
  8. Android(安卓)TabHost使用、动态加载内容
  9. Android异步加载图像小结 (含线程池,缓存方
  10. Android开发之消息处理机制(一)——Handler