I am working on Android app and I have receive a broadcast for incoming calls and I need to create a notification in the notification tray.

I am trying the following

NotificationManager nm = (NotificationManager)this;

But this displays an error Cannot cast from PhoneStateManager to NotificationManager.

I've also tried using context instead of this but then says it can't cast from PhoneStateManager to NotificationManager.

How can I create the notification from the PhoneStateListener, is this possible or do I need to pass it to a service?

有帮助吗?

解决方案

Get an instance of it like the docs say:

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

Since you didn't post more code, it's hard to tell what you can use for context. If you do have a Context variable, use it. If your listener is contained in an Activity/Service do MyActivity.this or MyService.this instead of the context variable that I used in my code snippet.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top