Android 源代码中的@hide 是什么意思? [英] What does @hide mean in the Android source code?

查看:48
本文介绍了Android 源代码中的@hide 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Activity 源代码,第 3898 行(靠近底部):

For the Activity source code, line 3898 (close to the bottom):

/**
 * @hide
 */
public final boolean isResumed() {
    return mResumed;
}

@hide 是什么意思?

我发现我的 public class ChildActivity extends Activity { ... } 不能使用/查看 Activity.isResumed().这是正常的吗?我如何访问它?

I found my public class ChildActivity extends Activity { ... } cannot use/see Activity.isResumed(). Is this normal? How can I access it?

推荐答案

Android 有两种类型的 API,无法通过 SDK 访问.

Android has two types of APIs that are not accessible via SDK.

第一个位于包 com.android.internal 中.第二种 API 类型是用 @hide 标记的类和方法的集合Javadoc 属性.

The first one is located in package com.android.internal. The second API type is a collection of classes and methods that are marked with the @hide Javadoc attribute.

从 Android 9(API 级别 28)开始,Google 引入了对使用非 SDK 接口的新限制,无论是直接使用、通过反射还是通过 JNI.每当应用程序引用非 SDK 接口或尝试使用反射或 JNI 获取其句柄时,就会应用这些限制.

Starting from Android 9 (API level 28), Google introduces new restrictions on the use of non-SDK interfaces, whether directly, via reflection, or via JNI. These restrictions are applied whenever an app references a non-SDK interface or attempts to obtain its handle using reflection or JNI.

但是在 API 级别 28 之前,仍然可以通过 Java 反射访问隐藏的方法.@hide 属性只是 Javadoc(也包括 droiddoc)的一部分,所以 @hide 只是意味着方法/类/字段从 API 文档中排除.

But before API level 28, the hidden methods could still be accessed via Java reflection. The @hide attribute is just part of Javadoc (droiddoc also), so the @hide just simply means the method/class/field is excluded from the API docs.

例如ActivityManager.java中的checkUidPermission()方法使用了@hide:

/** @hide */
public static int checkUidPermission(String permission, int uid) {
    try {
        return AppGlobals.getPackageManager()
                .checkUidPermission(permission, uid);
    } catch (RemoteException e) {
        // Should never happen, but if it does... deny!
        Slog.e(TAG, "PackageManager is dead?!?", e);
    }
    return PackageManager.PERMISSION_DENIED;
}

但是,我们可以通过反射来调用它:

However, we can call it by reflection:

Class c;
c = Class.forName("android.app.ActivityManager");
Method m = c.getMethod("checkUidPermission", new Class[] {String.class, int.class});
Object o = m.invoke(null, new Object[]{"android.permission.READ_CONTACTS", 10010});

这篇关于Android 源代码中的@hide 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆