android - 新的@SystemApi注解是什么意思,和@hide有什么区别?

标签 android annotations

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 codesetWifiApEnabledgetPrivilegedConfiguredNetworks 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/

相关文章:

android - 如何在不指定 URL 的情况下打开默认的 android 浏览器?

java - 我的代码有什么错误,它没有显示弹出框(Android)?

java - 生成文档的 Java 注释的生命周期是哪个?

typescript - Typescript 中装饰器的别名

java - 我可以在实体类中有一个方法而不映射到数据库吗

java - 使用半径搜索附近的物体。谷歌地图

android - 为 Google Android 应用内购买伪造签名字符串

java - 当在方法级别内部使用 @PreAuthorize 或 @PostAuthorize 时,Class.getAnnotations() 不会返回类级别注释的完整列表

java - Spring MVC @RequestMapping注解中Path和Variable的区别

android - import android.support.v13.app.FragmentActivity 无法解析