0

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.

Doopy
  • 636
  • 8
  • 21
  • `registerReceiver` will just run in the main Activity thread. Define the broadcast receiver in your manifest if you want it to trigger outside of your Activity. http://stackoverflow.com/a/10876080/833647 – Ken Wolf Feb 21 '15 at 15:04

1 Answers1

0

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>
Marcus
  • 6,697
  • 11
  • 46
  • 89
  • Thanks, but do i have to create a class extending Broadcastreceiver then because I have to sepcify a name in the manifest? – Doopy Feb 21 '15 at 15:51
  • Yes, you create a new Java class that extends BroadcastReceiver, and you put the name of that class in your manifest. @DQQpy – Marcus Feb 21 '15 at 15:52
  • By the way, I solved it already by calling register() on the application context, is there any difference between this and declaring the receiver in the manifest? – Doopy Feb 21 '15 at 15:53
  • By declaring it in the manifest, your application does not have to be running to receive call to its `onReceive` method. @DQQpy – Marcus Feb 21 '15 at 15:54