带广播接收器的Android报警管理器

带广播接收器的Android报警管理器,android,service,notifications,broadcastreceiver,alarmmanager,Android,Service,Notifications,Broadcastreceiver,Alarmmanager,你好,我有我的报警管理器显示通知。问题是,一旦触发报警并显示通知,当执行MainActivity(super.onCreate)代码时,它总是触发通知 这是我执行报警的主要活动 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initAlarm

你好,我有我的报警管理器显示通知。问题是,一旦触发报警并显示通知,当执行MainActivity(super.onCreate)代码时,它总是触发通知

这是我执行报警的主要活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initAlarm();

}

private void initAlarm(){

    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.MONTH, 8);
    calendar.set(Calendar.YEAR, 2015);
    calendar.set(Calendar.DAY_OF_MONTH, 21);

    calendar.set(Calendar.HOUR_OF_DAY, 15);
    calendar.set(Calendar.MINUTE, 45);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.AM_PM,Calendar.PM);

    //Creo un intent que ejecutara el BroadcastReceiver
    Intent myIntent = new Intent(MainActivity.this, AlarmBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
这里是AlarmBroadcastReceiver,它应该仅在AlarmManager的时间到期时调用

public class AlarmBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startIntent = new Intent(context, AlarmService.class);
        context.startService(startIntent);
    }
}
广播接收器启动的服务:

public class AlarmService extends Service{
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        generarNotificacion();

        return Service.START_NOT_STICKY;
    }

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public void generarNotificacion(){

    Intent resultIntent = new Intent(this.getApplicationContext(), MainActivity.class);

    android.support.v4.app.NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.logo)
                    .setContentTitle(getResources().getString(R.string.app_name))
                    .setContentText(getResources().getString(R.string.texto_notificacion));

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    mBuilder.setContentIntent(resultPendingIntent);

    // Sets an ID for the notification
    int mNotificationId = 001;

    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

}
最后,我将此代码添加到manifest.xml中

<application
        android:allowBackup="true"
        android:icon="@mipmap/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    <service android:name=".AlarmService"
        android:enabled="true" />

    <receiver android:name=".AlarmBroadcastReceiver"/>

...

</application>

...

这是显而易见的。您正在onCreate中调用initAalarm()。要理解,请使用您的代码执行以下测试用例:

假设当前日期和时间为2015年9月20日下午1:00 警报设置为未来日期,即当前时间后15分钟,即2015年9月20日下午1:15
现在要测试它,您可以等待时间到达,也可以更改系统日期时间。执行此操作后,您将看到通知在同一时间被触发。现在不要更改initAlarm()中的任何内容,关闭活动并再次启动它,您将再次看到通知。这背后的原因是,如果报警设置为与系统时间相关的过去日期,则会立即触发报警。


请参阅您在9月份(即第9个月)发布的文档 其中as
您正在过去的日期尝试此操作,该日期为

calendar.set(Calendar.MONTH, 8);
用未来日期更改它。