Exchange of information between Activity and Service (vice versa)

0

I am studying services on android and I have a little problem, I try to see how the exchange of information works both from the activy to the service and vice versa, I have no problems passing data from the activity to the service, but I got this form that is through a broadcast that does not execute the passage of data at the time I want.

This is my main class

package com.example.jesus.servicio;

// here are all the respective imports

public class MainActivity extends AppCompatActivity {

    Button btn ;

    public String algo = "Conteo Regresivo, Ejecutado en el Servicio";

 MyReceiver Recepcion;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = findViewById(R.id.button);

        Recepcion = new MyReceiver();
        IntentFilter obtener = new IntentFilter();
        obtener.addAction(servicio.MY_ACTION);
        registerReceiver(Recepcion, obtener);

//Boton que ejecuta el servicio

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//forma 1 de llamar a un servicio
//              startService(new Intent(getApplicationContext(),servicio.class));
//forma 2  de llamr a un servicio pero pasando datos
                Intent servicio = new Intent(MainActivity.this, servicio.class);
                servicio.putExtra("datos",algo);
                startService(servicio);
            }
        });
    }

// Metodo de Receptor
    private class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context hola , Intent valor) {
            // TODO Auto-generated method stub

            int datapassed = valor.getIntExtra("DATAPASSED", 0);

            Toast.makeText(MainActivity.this, "Los numeros vienen del Servicio!\n"
                            + "Numero: " + String.valueOf(datapassed)+ " fue pasado, del servicio",
                    Toast.LENGTH_LONG).show();

        }

    }
}

and here is my service

package com.example.jesus.servicio;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;



public class servicio extends Service {

    final static String MY_ACTION = "La accion a ejecutar";

    public static String TAG = "El servicio";

    public servicio() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "Inicio");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Intent enviar= new Intent();
        enviar.setAction(MY_ACTION);
        enviar.putExtra("DATAPASSED", 365);
        sendBroadcast(enviar);

        Log.d(TAG, "el body del servicio");
// Como obtener el dato de la forma 2 puede ser en Star command o en el Create o en el mismo Destroy.
        String datosObtenidos = intent.getStringExtra("datos");
        Toast.makeText(this, datosObtenidos, Toast.LENGTH_SHORT).show();
            int i;
            for(i=2; i>=0; i--){
                Log.d(TAG, "Cuenta: "+i);
                Toast.makeText(this, ""+ i, Toast.LENGTH_SHORT).show();
            }



        stopSelf();

//Esto funciona para retornar algo q se desee de este servicio
       return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }



}

Note that in my service I try to perform the method of sending the data, before the method that receives data and executes a simple counting code.

What am I doing wrong? I want the shipping method to run first and send the data to the activity and then the service receives the activity data and performs the process indicated there ..

    
asked by Jesus Cabrita 30.09.2018 в 18:00
source

0 answers