2

以下代码不起作用。也试试这个而不是getapplicationcontext。我需要一个提示:

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 后如何删除通知?

更新: 我解决了一个错误。但是还有一个错误:

截屏

4

2 回答 2

2

用 context.getSystemService 替换 getSystemService!

于 2012-10-25T21:58:02.550 回答
0

BroadcastReceivers 没有上下文,所以你不能这样做。

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

于 2012-10-23T15:03:01.413 回答