My alarm doesn't trigger when I finish the activity before. When I just stay in the activity, the alarm works fine. Here's the code:
AlarmManager alarms = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
final BroadcastReceiver receiver_daily = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "DAILY BONUS");
String title = getResources().getString(R.string.playreminder_daily_title);
String text = String.format(getResources().getString(R.string.playreminder_daily_text), getResources().getInteger(R.integer.daily_bonus_coins));
showDailyBonusNotification(title, text);
unregisterReceiver(this);
Account.setBonusAvailable(true, getApplicationContext());
}
};
registerReceiver(receiver_daily, new IntentFilter("com.doopy.numbers.ACTION_PLAYREMINDER_DAILY"));
PendingIntent operation = PendingIntent.getBroadcast(getApplicationContext(), RQC_BROADCAST, new Intent("com.doopy.numbers.ACTION_PLAYREMINDER_DAILY"), 0);
alarms.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+DAILY_BONUS_TIME, operation);
addCoinsAnimated(Account.getCoins(getApplicationContext()), getResources().getInteger(R.integer.daily_bonus_coins), 500, true, getApplicationContext());
Account.setBonusAvailable(false, getApplicationContext());
mGetBonusLayout.setVisibility(View.GONE);
I also noticed that I get this leak warning:
android.app.IntentReceiverLeaked: Activity com.doopy.numbers.GameOverActivity has leaked IntentReceiver com.doopy.numbers.GameOverActivity$5@41c33780 that was originally registered here. Are you missing a call to unregisterReceiver()?
I don't want to unregister my alarm after the activity has finished/is destroyes, because it's supposed to trigger a notification that the daily bonus in now available, although the application might not be running.
Regarding the leak warning, you can define your receiver in your manifest like so
<receiver android:enabled=["true" | "false"]
android:exported=["true" | "false"]
android:icon="drawable resource"
android:label="string resource"
android:name="string"
android:permission="string"
android:process="string" >
. . .
</receiver>
this will give you the expected behavior. Reference here.
An example:
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="com.doopy.numbers.ACTION_PLAYREMINDER_DAILY" />
</intent-filter>
</receiver>