java - Android 警报管理器和通知

标签 java android notifications alarmmanager

以下代码不起作用。也可以尝试这个而不是 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 后删除通知?

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

Screenshot

最佳答案

将 getSystemService 替换为 context.getSystemService!

关于java - Android 警报管理器和通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13032259/

相关文章:

java - java中生成随机数的switch语句: not working

java - 具有 GWT 身份验证问题的 Dropbox Java API

Android 短信拦截无通知图标或 WAP-PUSH 消息

java - Spring:@Resource 注入(inject)在 JDK9 下停止工作

java - ExpandableListView 中的静态子项

android - 更改 ActionBar 标题

java - 从另一个布局(ViewPager)获取按钮?

ios - 私有(private)数据库和订阅的 Cloudkit 问题?

ios - 大量本地通知swift 3

java - 多个 JLabel 上的鼠标监听器