2014-11-05 32 views
11

Android最近在其SDK源代码中引入了@SystemApi。看起来和之前的@hide注解一样有效,因为它们也从SDK jar类中剥离。新@SystemApi注释的含义是什么,与@hide有什么不同?

应用程序是否有可能以不同于旧的@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 
*/ 

回答

0

中的方法@SystemApi注释是那些与@hide的子集。 这显然是内部团队(也可能是合作伙伴)的一个指标,即这些方法是实际的API,但不适用于公共开发者。

因此,@SystemApi方法比@hide方法更稳定,在未来任何时候都可以进行更改,而无需考虑任何兼容性,并且任何OEM都可以根据自己的意愿更改它们。

如果您尝试通过反射调用内部API,则始终首选@SystemApi方法以实现更好的未来兼容性。

15

@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中,setWifiApEnabledgetPrivilegedConfiguredNetworks的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; 
    } 
} 
+0

感谢您的解释!在我的测试中,大多数使用'@SystemApi'和'@ hide'注释的API(以前只有'@ hide'注解)仍然可以通过反射访问。你的情况下'InvocationTargetException'的详细信息是什么? – 2014-12-07 11:57:01

+0

我使用Android 5.0在Nexus 5上做了实验。 @ oasis-feng我猜''SystemApi'的行为是版本相关的? – 2015-01-12 03:05:17

+0

我还使用Android 5.0.2在Nexus 5上测试了它。也许它不同于API到API。你可以粘贴你的InvocationTargetException的详细消息吗? – 2015-01-12 08:43:39