Skip to content Skip to sidebar Skip to footer

Alarmmanager Not Called Next Day If The App Sleeps For Abt 1 Day

I am developing an android app which shows a notification every 12 hour if the time is saved in the database. So everytime a data is entered or edited in the database ,I cancel the

Solution 1:

You don't need to call alarm_manager.cancel(sender); if you set the PendingIntent.FLAG_CANCEL_CURRENT.

Your call to

alarm_manger.setRepeating(AlarmManager.RTC_WAKEUP, now, AlarmManager.INTERVAL_HALF_DAY, sender);

will trigger the alarm right away, since the now is already passed when you set the alarm.

I suggest you use

now + DateUtils.HOUR_IN_MILLIS / 2 

for the triggerAtMillis parameter

Did you tried to schedule it for smaller interval? Does it get triggered ?

Solution 2:

After having seen your Alarm_Manager code, I think it is illegal to do this in your BroadcastReceiver object directly. Quote:

If this BroadcastReceiver was launched through a tag, then the object is no longer alive after returning from this function.

I believe there is no other way than to create a Service which is informed by your BroadcastReceiver, and make sure that the Service calls setLatestEventInfo() with itself (this) as the Context.

The reason why your asynchronous Broadcast fails while it works when your app is running is probably that the Context provided to the BroadcastReceiver lives only for the duration of the call to the BroadcastReceiver when your app does not run. So the Notification service, which only runs after your BroadcastReceiver has died along with the temporary context, is missing a valid context.

When your app runs, the Broadcast probably comes with your Activity or Application object as Context, and this is still vaild when the Notification manager runs.

Hope this helps.

Update: An `IntentService`` will do. You don't want a full time Service for that.

Update 2: Some snippets.

<service android:name=".MyIntentService" android:label="@string/my_intent_service_name" />

publicfinalclassMyIntentServiceextendsIntentService {
    publicMyIntentService() {
    super("service name");
    // set any properties you need
    }
    @OverridepublicvoidonCreate() {
        super.onCreate();
        // do init, e.g. get reference to notification service
    }
    @OverrideprotectedvoidonHandleIntent(Intent intent) {
    // handle the intent// ...especially:
        notification.setLatestEventInfo(this, "App", "Notify" , contentIntent);
        // ...
    }
}

publicfinalclassMyAlarmReceiverextendsBroadcastReceiver {
    @OverridepublicvoidonReceive(Context context, Intent intent) {
        context.startService(newIntent(context, MyIntentService.class));
    }
}

Post a Comment for "Alarmmanager Not Called Next Day If The App Sleeps For Abt 1 Day"