关于修改Android标准WIFI的API

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LEAD_SOLO/article/details/51570327

今天做项目遇到一些奇葩的需求,没办法嘛,需求就是需求。总是有价值存在的。需求的内容是,通用的WIFI开关每个软件都能使用,而该功能在某些场合就是事故。因此需要自行封装WIFI开关API使标准的API无效,只有特定的API才能有效。

  从WifiManager入手,其内部有一个setWifiEnabled(boolean );要开启使用WIFI都必须调用该函数,我们先看看该函数原型

 /**
      * Enable or disable Wi-Fi.
      * @param enabled {@code true} to enable, {@code false} to disable.
      * @return {@code true} if the operation succeeds (or if the existing s
     tate
      *         is the same as the requested state).
      */
     public boolean setWifiEnabled(boolean enabled) {                       
         if (mAppOps.noteOp(AppOpsManager.OP_WIFI_CHANGE) !=
                 AppOpsManager.MODE_ALLOWED)//进行一个简单的判断是否有权限修改否则返回false
             return false;
         try {
             return mService.setWifiEnabled(enabled);//传入wifiserver进行后续wifi开启操作。
         } catch (RemoteException e) {
             return false;
         }
     }
内容很少很简单。那么我们可以将该函数始终返回一个false。自定一个新的函数来实现该函数所实现的内容。如下

 /**
      * Enable or disable Wi-Fi.
      * @param enabled {@code true} to enable, {@code false} to disable.
      * @return {@code true} if the operation succeeds (or if the existing s
     tate
      *         is the same as the requested state).
      */
     public boolean setWifiEnabled(boolean enabled) {                       
	return enabled ? false : false;//标准方法返回false<pre name="code" class="java">     }
 
 
 /**
      * Enable or disable Wi-Fi.
      * @param enabled {@code true} to enable, {@code false} to disable.
      * @return {@code true} if the operation succeeds (or if the existing s
     tate
      *         is the same as the requested state).
      */
     public boolean setWifiEnabledForXXXX(boolean enabled) {//封装的方法实现标准方法的功能                       
         if (mAppOps.noteOp(AppOpsManager.OP_WIFI_CHANGE) !=
                 AppOpsManager.MODE_ALLOWED)//进行一个简单的判断是否有权限修改否则返回false
             return false;
         try {
             return mService.setWifiEnabled(enabled);//传入wifiserver进行后续wifi开启操作。
         } catch (RemoteException e) {
             return false;
         }
     }
这样只要系统开发人员不告诉应用人员应该调用setWifiEnabledForXXX方法的话就永远无法开启WIFI功能。当然这需要编译新的system.img并且需要生成sdk给应用人员调用

具体方法就是make PRODUCT-sdk-sdk

生成的sdk在out/host/linux-x86/sdk目录下 将sdk目录下的android.jar替换AndroidStudio工程里的sdk里android.jar就可以调用新的API了。


猜你喜欢

转载自blog.csdn.net/LEAD_SOLO/article/details/51570327