以下代码不起作用。也可以试试这个,而不是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后删除通知?
更新: 我解决了一个错误。但仍然有一个错误:
定义“不起作用” – kosa
我已经解决了问题! - >只需将getSystemService替换为context.getSystemService! – user1756209
好的。我会建议发布回答并接受它。享受编码。 – kosa