@SystemApi
,@PrivateApi
和@hide
据this commit,@SystemApi
是老@PrivateApi
重命名。标记为@hide
的API不一定是@SystemApi
,但@SystemApi
需要@hide
。
有关@hide
javadoc注释的更多信息,this post给出了一个很好的答案。
根据我自己的实验,一个(非系统应用程序)仍然可以访问@hide
API和领域使用Java反射像(来自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);
P.S.
在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;
}
}
感谢您的解释!在我的测试中,大多数使用'@SystemApi'和'@ hide'注释的API(以前只有'@ hide'注解)仍然可以通过反射访问。你的情况下'InvocationTargetException'的详细信息是什么? – 2014-12-07 11:57:01
我使用Android 5.0在Nexus 5上做了实验。 @ oasis-feng我猜''SystemApi'的行为是版本相关的? – 2015-01-12 03:05:17
我还使用Android 5.0.2在Nexus 5上测试了它。也许它不同于API到API。你可以粘贴你的InvocationTargetException的详细消息吗? – 2015-01-12 08:43:39