@SystemApi、@PrivateApi 和 @hide
根据this commit,@SystemApi是旧@PrivateApi的重命名。标记为@hide 的API 不一定是@SystemApi,但@SystemApi 需要@hide。
更多关于@hidejavadoc注解的信息,this post给出了很好的答案。
根据我自己的实验,一个(非系统应用程序)仍然可以使用 Java 反射访问@hide API 和字段,例如(来自this post):
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = new WifiConfiguration();
config.SSID = "AccessPointSSID";
Method method = manager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(manager, config, true);
但是试图访问@SystemApi事物使用Java反射是不可能的(以下代码将触发invocationTargetException):
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method method = manager.getClass().getMethod("getPrivilegedConfiguredNetworks");
List<WifiConfiguration> configs = (List<WifiConfiguration>)method.invoke(manager);
附言
在WifiManager java code 中,setWifiApEnabled 和 getPrivilegedConfiguredNetworks API 定义为:
/**
* Start AccessPoint mode with the specified
* configuration. If the radio is already running in
* AP mode, update the new configuration
* Note that starting in access point mode disables station
* mode operation
* @param wifiConfig SSID, security and channel details as
* part of WifiConfiguration
* @return {@code true} if the operation succeeds, {@code false} otherwise
*
* @hide Dont open up yet
*/
public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
try {
mService.setWifiApEnabled(wifiConfig, enabled);
return true;
} catch (RemoteException e) {
return false;
}
}
和
/** @hide */
@SystemApi
public List<WifiConfiguration> getPrivilegedConfiguredNetworks() {
try {
return mService.getPrivilegedConfiguredNetworks();
} catch (RemoteException e) {
return null;
}
}