警报管理器未触发

基兰·斯内普斯(Kieran Snapes)

我是新手,所以我有点迷路。

我已经构建了一个通知,并将其作为未决的Intent传递给警报管理器,但是,与其等待间隔触发警报并显示通知,不如立即显示该通知。我做错了什么,导致警报设置不正确?

public class NotificationController{

Context context;

public void createNotification(Context context){

    this.context = context;
    Notification notification = getNotification();

    //creates notification
    Intent intent = new Intent(context, NotificationReceiver.class);
    intent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1);
    intent.putExtra(NotificationReceiver.NOTIFICATION, notification);
    PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    //schedules notification to be fired.
    AlarmManager  alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10000 , pIntent);

}


private Notification getNotification(){

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);

    Notification.Builder builder = new Notification.Builder(context)
            .setContentTitle("Reminder")
            .setContentText("Car service due in 2 weeks")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent);
    return builder.build();
}
}

我的接收者

public class NotificationReceiver extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";

public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = intent.getParcelableExtra(NOTIFICATION);
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(id, notification);

}
}

我也已<receiver android:name=".NotificationReceiver" >在AndroidManifest中注册

行军回家

在这条线

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10000 , pIntent);

您指定警报必须在启动设备后的10秒钟后触发(这是过去的记录,因此警报将立即触发)。如果您希望在设置警报后10秒钟使用它,请使用自设备启动以来的毫秒数,再加上10秒钟:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() + 10000 , pIntent);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

警报管理器未触发

来自分类Dev

警报管理器立即触发

来自分类Dev

警报管理器不触发广播

来自分类Dev

警报管理器不会触发

来自分类Dev

关闭应用程序时未触发Android警报管理器

来自分类Dev

警报管理器类出现问题。我正在设置闹钟,但当时未触发

来自分类Dev

警报管理器触发的待定意图似乎立即触发

来自分类Dev

警报管理器触发的待定意图似乎立即触发

来自分类Dev

应用程序处于打ze模式时,警报管理器触发的Android通知未触发

来自分类Dev

警报管理器设置重复随机触发

来自分类Dev

警报管理器从未在Android中触发

来自分类Dev

时间过后,android警报管理器会触发

来自分类Dev

Prometheus警报管理器配置未显示在UI中

来自分类Dev

警报管理器 BrodcastReceiver

来自分类Dev

无法获得警报管理器来触发蓝牙设备的扫描

来自分类Dev

为什么警报管理器不断触发未决的意图 android?

来自分类Dev

警报管理器仅触发 Android 中的最后一个计划任务

来自分类Dev

警报管理器和广播接收器未停止

来自分类Dev

警报管理器不显示通知

来自分类Dev

如何编辑/重置警报管理器?

来自分类Dev

通知和警报管理器

来自分类Dev

警报管理器无法设置

来自分类Dev

在Android中取消警报管理器

来自分类Dev

在Android中设置警报管理器

来自分类Dev

警报管理器立即运行

来自分类Dev

警报管理器运行活动

来自分类Dev

在Android中设置警报管理器

来自分类Dev

警报管理器不起作用

来自分类Dev

警报管理器显示异常