简体   繁体   English

不能使用公共静态方法?

[英]Can't use a public static method?

This is confusing. 这很混乱。 I'm look at the Android 2.2.2_r1 source code for the NotificationManager class, and I see the method getService() which is defined as public and static . 我看一下NotificationManager类的Android 2.2.2_r1源代码 ,并且看到定义为publicstatic getService()方法。 However, eclipse is telling me: 但是,日食告诉我:

The method getService() is undefined for the type NotificationManager on the line 该行上的NotificationManager类型的getService()方法未定义

 Object o = NotificationManager.getService();

My project is building against Android 2.2/ API Level 8. I tried to use reflection to see the method names and modifiers, and sure enough, I got back 我的项目是针对Android 2.2 / API Level 8构建的。我尝试使用反射来查看方法名称和修饰符,并且确实,我回来了

public static getService

Am I missing something here? 我在这里想念什么吗? Why would eclipse tell me this method doesn't exist? 为什么日食会告诉我这种方法不存在?

You will find a very detailed answer in this post . 您将在这篇文章中找到非常详细的答案。

In short: Because you compile against the android.jar , which has all hidden methods (like the one you are trying to access) removed. 简而言之:因为您是针对android.jar编译的,所以android.jar删除了所有隐藏方法(例如您尝试访问的方法)。 They will only be there on runtime, for internal android usage. 它们仅在运行时存在,供内部android使用。


But since you perhaps also need it. 但是既然您也许也需要它。 The right way to access the NotificationManager is via the getSystemService method of a context: 访问NotificationManager的正确方法是通过上下文的getSystemService方法:

NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);

With context being a valid context (like your current activity). context是有效上下文(例如您当前的活动)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM