Android Alarm Manager with Broadcast receiver

梦想的初衷 提交于 2019-12-11 10:44:22

问题


Hello I have my Alarm Manager to show a Notification. The problem is that once the alarm is triggered and the notification is shown, when the code of MainActivity(super.onCreate) is executed, it always triggers the notification.

Here is my MainActivity which executes the Alarm.

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

Here is the AlarmBroadcastReceiver which is supposed to be called only when the time of the AlarmManager expires.

public class AlarmBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startIntent = new Intent(context, AlarmService.class);
        context.startService(startIntent);
    }
}

The service launched by the BroadcastReceiver:

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

}

And finally I have added this code to the 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>

回答1:


It is obvious. You are calling initAalarm() in onCreate. To understand execute following test case with your code:

Lets say current date and time is 20 Sep 2015 1:00pm and Alarm is set to future date say 15 min after current time i.e. 20 Sep 2015 1:15pm
Now to test it you can either wait for time to arrive or change system date-time. After doing this you will see notification is fired on same time.Now don't change anything in initAlarm() , close activity and again start it , you will see notification again. The reason behind this is if alarm is set to some past date with respect system time then it is immediately fired.

See documentation of Alarm Manager's set method




回答2:


You Posted this in month of September which is 9th Month
where as
You are trying this in past date which is

calendar.set(Calendar.MONTH, 8);

change it with future date.



来源:https://stackoverflow.com/questions/32697295/android-alarm-manager-with-broadcast-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!