1

I call a BroadcastReceiver from an Activity

Intent alarmIntent = new Intent(MainActivity.this, AlarmRec.class);
            alarmIntent.putExtra("lol",0);
            pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
            AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            manager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, pendingIntent);

Its onReceivemethod is this:

        int lol=intent.getExtras().getInt("lol");
    Toast.makeText(context, "I'm running "+lol, Toast.LENGTH_SHORT).show();
    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    lol++;
    Intent alarmIntent = new Intent(context, AlarmRec.class);
    alarmIntent.putExtra("lol",lol);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
    manager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, pendingIntent);

Basially, every time the Receiver is called, it should show a Toast It's Running plus an incremented value(1,2,3 etc). However, it always shows It's Running 0. What am I doing wrong?

Orcha
  • 75
  • 1
  • 1
  • 8

1 Answers1

1

Because lol isn't getting incremented!

Your concept is totally wrong ~

int lol and String lol are different objects. You have to pass the same object in both areas like below.

Class 1 : (Activity)

....
AlarmManager manager =...
....
manager.setRepeating(.....); //use to repeat + don't need to pass lol

Class 2 : (Receiver)

.... 
public static int LOL = 0;
....
onReceive(....){
LOL++;
Toast.makeText(....,(String)LOL,....).show();
}

Otherwise

Increment lol in Class 1, then send the value to Class 2. Just implement toast in Class 2.

Like :

int lol =0
for(;;) {   //a forever loop which runs until your device die
sendToClass2(lol++);
}

private void sendToClass2(int foe){
....
alarmIntent.putExtra("lol",foe);
....
manager.setExact(...); // If you really love to use seExact use this
}

Besides that, why you're trying to launch 2nd class from the code exists in itself? Isn't a bad and complex idea, even not working as your question suggest.

exploitr
  • 843
  • 1
  • 14
  • 27
  • First solution will not work, broadcast receivers are destroyed after onReceive, next call will also start from 0. I am not sure what you are doing in the second solution, but I think it will create the same problem. I use setExact to make sure that the code happens frequently and on time, I don't know when setRepeating will execute. Basically, I am interested in comparing a location from gps with the previous one, this code is a test to see how to save values between calls. I know I should use locationManger/fusedlocationAPI but the results are problematic – Orcha Jul 05 '18 at 18:13
  • You want to save values between calls? Then why a variable? Please use `SharedPreference` & your concept is wrong: **Context-registered receivers receive broadcasts as long as their registering context is valid** (from the docs) so, a BroadcastReceiver isn't destroyed after a `onReceived` call : Further on it check my answer : https://stackoverflow.com/a/50628103/8572503 – exploitr Jul 05 '18 at 18:38
  • Your procedure is too much complex , your work can be done in an easy manner – exploitr Jul 05 '18 at 18:40
  • so, if I manually register a receiver, its variables will remain. Thanks for that – Orcha Jul 05 '18 at 19:24
  • @Orcha , you're welcome. Feel free to ask for further help. – exploitr Jul 06 '18 at 03:54