以下代码不起作用。也试试这个而不是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 后如何删除通知?
更新: 我解决了一个错误。但是还有一个错误: