2013-02-06 36 views
0

我正在使用android應用程序,並且我收到來電的廣播,我需要在通知托盤中創建通知。從PhoneStateListener創建通知

我想下面

NotificationManager nm = (NotificationManager)this; 

但這顯示錯誤Cannot cast from PhoneStateManager to NotificationManager

我也試過使用context而不是this,但後來說它不能從PhoneStateManager轉換爲NotificationManager

如何從PhoneStateListener創建通知,這是可能的還是我需要將它傳遞給服務。

任何幫助,非常感謝。

回答

2

得到它like the docs say一個實例:

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

既然你沒有張貼更多的代碼,這是很難告訴你可以使用什麼context。如果您確實有Context變量,請使用它。如果您的聽衆包含在「活動/服務」MyActivity.thisMyService.this中,而不是我在代碼片段中使用的context變量中。