Guys, With Matthew Johnson hint, I SOLVED the problem. I was not
setting the boolean flag to false when my Parent Class onDestroy
method. So when I was intent to unbind the service the service doesn't
exists anymore. With a boolean flag my thread dont start again and the
method dont call the unbindService.


The class [Service with Thread]

I put this instructions to correct the bug:
      public void onCreate() {
                state = true;
                new Thread(this).start();
        }

        public void onDestroy() {
                state = false;
        }


 [Main Activity]
//bindingService
public void onClick(View v) {
                                if (!state) {
                                        Class classeServico = 
ServicoComConexao.class;
                                        bindService(new 
Intent(ExemploBindService.this,
                                                        classeServico), 
conexao, Context.BIND_AUTO_CREATE);
                                        state = true;
                                }

unbindingService

public void onClick(View v) {
                                if (state){
                                  unbindService(conexao);
                                }
                        }

2011/6/11 The NaP <[email protected]>:
> Hello guys, I start a thread with a simple counter from 1 to 1000. My
> interface has Start and Stop functions.
> I want to stop the counter but when I click on Stop my App is
> returning the exception
>  "java.lang.IllegalArgumentException: Service not registered ".
>
> Anyone knows how to solve this problem.
> I using the interface Runnable...
>
> Thanks...
>
> [Main Activity] - Not the entire code
>
> public class ServiceConnectionMain extends Activity implements
> ServiceConnection{
>
>
>
>     //interface ServiceConnection
>        final ServiceConnection conexao = this;
>
>        //Iniciar servico
>        Button btStart = (Button) findViewById(R.id.btnstart);
>        btStart.setOnClickListener(new Button.OnClickListener(){
>            public void onClick( View v ){
>
>                //cria uma intent para a classe de servico
>                Class classeServico = ServicoComConexao.class;
>                Intent itService = new
> Intent( ServiceConnectionMain.this, classeServico);
>
>                bindService( itService, conexao,
> Context.BIND_AUTO_CREATE );
>
>                }
>        });
>
>        //Parar Servico
>        Button btStop = (Button) findViewById(R.id.btnstop);
>        btStop.setOnClickListener(new Button.OnClickListener(){
>            public void onClick(View v){
>                    unbindService( conexao );
>                    mBound = false;
>            }
>        });
>    }
>
>    public void onServiceConnected(ComponentName className, IBinder
> service) {
>        // Recupera a interface para interagir com o serviÁo
>        LocalBinder binder = (LocalBinder) service;
>        contador = binder.getService();
>    }
>
>    public void onServiceDisconnected(ComponentName className) {
>        contador = null;
>    }
> }
>
>
> [Service with Thread] not entire code
>
>
> public class ExemploServico extends Service implements Runnable {
>
>    private static final String CATEGORIA = "exemplo";
>
>    protected int count;
>    private static final int MAX = 1000;
>    private boolean ativo;
>
>    public void run() {
>        // TODO Auto-generated method stub
>        ativo = true;
>        while (ativo && count <= MAX){
>            Log.i(CATEGORIA, "Contador está em: " + count);
>            doThing();
>            count++;
>        }
>
>    }
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en



-- 
Eduardo Luz

talk:    [email protected]
msn:   [email protected]
Tumb: luzeduardo.tumblr.com
         21 7292-4741
          21 8404-7270

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to