2012-10-23 51 views
2

以下代码不起作用。也可以试试这个,而不是getapplicationcontext。我需要提示:Android Alarmmanager和通知

public class AlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) {  
     NotificationCompat.Builder nb = new NotificationCompat.Builder(); 
     nb.setContentTitle("title"); 
     nb.setContentText("message"); 
     nb.setSmallIcon(R.drawable.ic_launcher); 
     NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     final Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
     final PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0); 
     nb.setContentIntent(contentIntent); 
     Notification notification = nb.getNotification(); 
     nm.notify(0, notification); 
     } 

    } 

如何在onclick后删除通知?

更新: 我解决了一个错误。但仍然有一个错误:

Screenshot

+0

定义“不起作用” – kosa

+0

我已经解决了问题! - >只需将getSystemService替换为context.getSystemService! – user1756209

+0

好的。我会建议发布回答并接受它。享受编码。 – kosa

回答

2

将getSystemService替换为context.getSystemService!

0

BroadcastReceivers没有上下文,所以你不能做到这一点。

您应该将收到的意图转发给服务或活动,并让该类可以处理该通知。