Sunday, June 12, 2011

[android-developers] Re: Stop Thread started by bindService

What did you to do to register the Service? Did you include a
<service> tag in AndroidManifest.xml?

For that matter, are you sure the Service is still connected when you
call unbindService()? I have got this same exception when calling
unbindService on a ServiceConnection object that was not connected
with any Service. I think this is why the sample Service in Local
Service Example at http://developer.android.com/reference/android/app/Service.html
uses booleans to 'shadow' the connection state. That is, they keep
track in these booleans to (among other things) avoid calling
unbibndService() on a connection that already went away.

At first, I thought you meant to use 'ativo' this way, but I see no
code to set it false, nor do you check in in onClick().

Finally, if these two suggestions do not help you find the problem,
please post more of your Exception stack trace, so that we can see
what API it called to get this Exception, and which or your APIs was
most recent on the stack.

On Jun 11, 6:40 am, The NaP <luz.edua...@gmail.com> wrote:
> 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment