简体   繁体   中英

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 . However, eclipse is telling me:

The method getService() is undefined for the type NotificationManager on the line

 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

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. They will only be there on runtime, for internal android usage.


But since you perhaps also need it. The right way to access the NotificationManager is via the getSystemService method of a context:

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

With context being a valid context (like your current activity).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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