Android 最近在其SDK 源代码中引入了@SystemApi。看起来与之前的 @hide 注释效果相同,因为它们也从 SDK jar 类中删除。
应用程序是否有可能以不同于旧的@hide API 的方式调用它们。
/**
* Indicates an API is exposed for use by bundled system applications.
* <p>
* These APIs are not guaranteed to remain consistent release-to-release,
* and are not for use by apps linking against the Android SDK.
* </p><p>
* This annotation should only appear on API that is already marked <pre>@hide</pre>.
* </p>
*
* @hide
*/
最佳答案
@SystemApi
、@PrivateApi
和 @hide
根据 this commit , @SystemApi
是旧的 @PrivateApi
的重命名。标记为 @hide
的 API 不一定是 @SystemApi
,但 @SystemApi
需要 @hide
。
有关@hide
javadoc 注释的更多信息,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);
但是尝试使用 Java 反射访问@SystemApi
东西是不可能的(以下代码将触发 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;
}
}
关于android - 新的@SystemApi注解是什么意思,和@hide有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26752656/