1

I have a an Activity A, and a class B(extends BroadCastReceiver). From A, I am making an alarm like this:

            Intent intent = new Intent(getApplicationContext(), TimerAlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
            alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

            Calendar time = Calendar.getInstance();
            time.setTimeInMillis(System.currentTimeMillis());
            time.add(Calendar.SECOND, 5);
            alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), 3000, pendingIntent);

and this this B(name is TimerAlarmReceiver):

public class TimerAlarmReceiver extends BroadcastReceiver { 
    public static long TIME;
    public static Boolean TimerOn=false;

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm went off", Toast.LENGTH_SHORT).show();
        Log.v("Tag", Long.toString(TimeKeeper.Time));
    }
    public static void setTime(long T)
    {           TIME=TimerActivity.DMILLIS;         
    }
}

What I want to do is access something like a static variable from onReceive. I tried making a public static member in the activity.The logs show the correct set value for as long as the app runs. But if I force quit the app, the log in onReceive becomes 0.

So how do I pass an Initial value to B ? Something that keeps the value even if the app is closed(force quit).

harveyslash
  • 5,906
  • 12
  • 58
  • 111

1 Answers1

0

You can pass it as one of the extras of the Intent you use to create the PendingIntent. It will then be delivered to your BroadcastReceiver and you can extract it there.

Another option is to store it somewhere.

matiash
  • 54,791
  • 16
  • 125
  • 154
  • I tried putting intent.put extra, but it does not seem to work. could you give the code to me ? – harveyslash Jul 03 '14 at 05:29
  • 1
    @harvey_slash Check [this post](http://stackoverflow.com/a/5256680/82788). You might need to specify an action for the extras to work. – matiash Jul 03 '14 at 05:32