you786
you786

Reputation: 3550

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?

Upvotes: 5

Views: 798

Answers (1)

Tim Roes
Tim Roes

Reputation: 1414

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).

Upvotes: 5

Related Questions