How to send data from a service to a main activity or MainActivity?

0

It turns out that I have a variable type int in my Mainactivity, I send it constantly to my service which is a music player, this variable allows me to change the order of the songs, when changing them in the service I need to also update them in my Mainactivity so that both my notification (service) and my main controls (MainActivity) are synchronized and the songs are not repeated, but I do not know how to do it

    
asked by Felipe Peña 21.11.2017 в 03:03
source

1 answer

0

Good morning Felipe.

One option you have is to declare the variable int as public and static to be able to access from another class (in this case a service) and thus update it.

public class MainActivity extends AppCompatActivity{
    public static int randomCode;
}

Then to access it from the service:

public class MyService extends Service {
    ...
    int serviceRandomCode; // Variable de control aleatorio del servicio
    MainActivity.randomCode = serviceRandomCode;
    ...
}
    
answered by 22.11.2017 в 11:51