报警管理器无法触发

时间:2014-11-24 11:01:58

标签: android alarmmanager android-notifications android-alarms

我是新手,所以我有点失落。

我已经建立了一个通知并将其作为pendingIntent传递给警报管理器,但是不是等待间隔触发警报并显示通知,而是立即显示通知。 我做错了什么,不允许正确设置闹钟?

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);

}
}

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

3 个答案:

答案 0 :(得分:3)

在这一行

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

您指定警报必须在设备启动后10秒内触发(过去,警报立即触发)。如果您在设置闹钟后10秒钟需要它,则使用自设备启动以来的毫秒数加10秒:

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

答案 1 :(得分:1)

试试这个:

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

答案 2 :(得分:0)

对我来说,老方法在几个小时后就会被杀死。请查看我用于不同操作系统版本的代码。可能会有帮助。

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(alarmTime, pendingIntent);
        alarmManager.setAlarmClock(alarmClockInfo, pendingIntent);
    }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

        alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
    }
    else {

        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
    } 

由于 艾尔沙德