1
I would like to send the result of a calculation back to Activity, I was trying to use a Binder as recommended, but I cannot recover this value. It would be possible to make Activity know that the service finished the calculation and then get the value?
My Activity:
public class MainActivity extends ActionBarActivity implements ServiceConnection{
private TextView numero1;
private TextView resultado;
private TextView numero2;
private TextView resultadosoma;
private EditText numero1informado;
private EditText numero2informado;
private Valores valores;
final ServiceConnection conexao = this;
final Intent it = new Intent("Service");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button biniciar = (Button) findViewById(R.id.button1);
Button bfechar = (Button) findViewById(R.id.button2);
numero1 = (TextView) findViewById(R.id.numero1);
numero2 = (TextView) findViewById(R.id.numero2);
resultado = (TextView) findViewById(R.id.resultado);
resultadosoma = (TextView) findViewById(R.id.resultadosoma);
numero1informado = (EditText) findViewById(R.id.numero1informado);
numero2informado = (EditText) findViewById(R.id.numero2informado);
biniciar.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
it.putExtra("numero1", numero1informado.getText().toString());
it.putExtra("numero2", numero2informado.getText().toString());
//startService(it);
Class<Service> classeServico = Service.class;
bindService(new Intent(MainActivity.this, classeServico), conexao, Context.BIND_AUTO_CREATE);
//classeServico.get
startService(it);
}
});
bfechar.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//stopService(it);
unbindService(conexao);
stopService(it);
}
});
}
@Override
protected void onDestroy() {
unbindService(conexao);
super.onDestroy();
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
LocalBinder binder = (LocalBinder) service;
valores = binder.getValores();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
valores = null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public TextView getNumero1() {
return numero1;
}
public void setNumero1(TextView numero1) {
this.numero1 = numero1;
}
public TextView getResultado() {
return resultado;
}
public void setResultado(TextView resultado) {
this.resultado = resultado;
}
public TextView getNumero2() {
return numero2;
}
public void setNumero2(TextView numero2) {
this.numero2 = numero2;
}
public TextView getResultadosoma() {
return resultadosoma;
}
public void setResultadosoma(TextView resultadosoma) {
this.resultadosoma = resultadosoma;
}
public EditText getNumero1informado() {
return numero1informado;
}
public void setNumero1informado(EditText numero1informado) {
this.numero1informado = numero1informado;
}
public EditText getNumero2informado() {
return numero2informado;
}
public void setNumero2informado(EditText numero2informado) {
this.numero2informado = numero2informado;
}
public Valores getValores() {
return valores;
}
public void setValores(Valores valores) {
this.valores = valores;
}
}
Service:
public class Service extends android.app.Service implements Runnable, Valores{
private static final int MAX = 10;
protected int cont = 0;
private boolean ativo;
private double numero1;
private double numero2;
private double resultado;
private final IBinder conexao = new LocalBinder();
public class LocalBinder extends Binder{
public Valores getValores(){
return Service.this;
}
}
public IBinder onBind(Intent intent){
return conexao;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i("and","onCreate()");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("and","onStartCommand()");
//cont = 0;
//ativo = true;
//new Thread(this,"Exemplo Serviço: "+startId).start();
String valor1 = intent.getStringExtra("numero1");
String valor2 = intent.getStringExtra("numero2");
Double numero1 = intent.getDoubleExtra("numero1", 0);
resultado = Double.valueOf(valor1) + Double.valueOf(valor2);
intent.putExtra("resultado", resultado);
return super.onStartCommand(intent, flags, startId);
}
public void run(){
while(ativo && cont < MAX){
fazAlgo();
Log.i("and", "Exemplo serviço executando: "+cont);
cont++;
}
}
public void fazAlgo(){
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i("and","onDestroy()");
ativo = false;
super.onDestroy();
}
@Override
public double numero1() {
// TODO Auto-generated method stub
return numero1;
}
@Override
public double numero2() {
// TODO Auto-generated method stub
return numero2;
}
@Override
public double resultado() {
// TODO Auto-generated method stub
return resultado;
}
}
It seems to be a relatively simple calculation to use in a service. Have you tried the calculation when passing to the other Activity or is it mandatory to use the service?
– itscorey
I was just making an example, to make a more complex calculation later. If it is a calculation that takes a long time to complete, putting this to be processed in the service the operating system will not close it, or it will have the same behavior as if to do it in Activity?
– Bruno
Service is for longer operations that run in the background, so use is correct. Activity can receive the result through Binder or through a broadcast receiver (the service can broadcast the result). If you want to use Binder, specify the problem you are having better. Saying "I’m not getting it" is not very clear.
– Piovezan
When the service finishes a calculation, as I warn Activity that it can catch the result?
– Bruno
In that case I would recommend a
BroadcastReceiver
to notify theActivity
of the end of the asynchronous calculation, but record aListener
in theService
is also another solution. It would be a simpler way than using aBinder
. I can put together an answer soon.– Wakim