Problem stopping a local service.

Put your problem here if it does not fit any of the other categories.

Problem stopping a local service.

Postby pmunoz » Fri Jan 21, 2011 1:02 pm

Hi, I have a problem stopping a local Service.
In detail, I have an activity that, starts, connects with a bindService,uses a service's methode for return a value and stops the service. Each operation managed by means of a button. Here is the code:

Code: Select all
    startService.setOnClickListener(new OnClickListener()
        {
         public void onClick(View button)
         {
            intentMessage=new Intent(myContext,FirstService.class);
            intentMessage.putExtra("parameter",333);
            startService(intentMessage);
         }
        });
       
        connectService.setOnClickListener(new OnClickListener()
        {
         public void onClick(View button)
         {            
            myServiceConnection=new ServiceConnection() {
               @Override
               public void onServiceDisconnected(ComponentName name)
               {
                  // This is called when the connection with the service has been
                    // unexpectedly disconnected -- that is, its process crashed.
                    // Because it is running in our same process, we should never
                    // see this happen.
                    Toast.makeText(myContext, "The connection was K.O.", Toast.LENGTH_LONG).show();
               }
               
               @Override
               public void onServiceConnected(ComponentName name, IBinder service)
               {

                    myService = ((FirstService.LocalBinder)service).getService();
                    Toast.makeText(myContext, "The connection was OK", Toast.LENGTH_LONG).show();
               }
            };
            myContext.bindService(intentMessage,myServiceConnection,Context.BIND_AUTO_CREATE);
         }
        });
       
        usingService.setOnClickListener(new OnClickListener()
        {
         public void onClick(View button)
         {
              connectionResult=myService.getTheValue();
              Toast.makeText(myContext,
                       "Invoquing: FirstService.getTheValue() which return: \n"+connectionResult,
                       Toast.LENGTH_LONG).show();
         }
        });
               
        stopService.setOnClickListener(new OnClickListener()
        {
         public void onClick(View button)
         {         
            myService.unbindService(myServiceConnection);
            myService.stopService(intentMessage);
            connectionResult=333;
            Toast.makeText(myContext, "Service State: Stopped", Toast.LENGTH_LONG).show();
         }
        });


The problem is that I have an exception when I want to unbind the Service before stop it.
This is the exception.


Code: Select all
01-21 12:02:17.572: ERROR/AndroidRuntime(901): Uncaught handler: thread main exiting due to uncaught exception
01-21 12:02:17.592: ERROR/AndroidRuntime(901): java.lang.IllegalArgumentException: Service not registered: es.upv.pros.androiddemos.MainActivity$2$1@437707c8
01-21 12:02:17.592: ERROR/AndroidRuntime(901):     at android.app.ActivityThread$PackageInfo.forgetServiceDispatcher(ActivityThread.java:880)
01-21 12:02:17.592: ERROR/AndroidRuntime(901):     at android.app.ApplicationContext.unbindService(ApplicationContext.java:865)
01-21 12:02:17.592: ERROR/AndroidRuntime(901):     at android.content.ContextWrapper.unbindService(ContextWrapper.java:324)
01-21 12:02:17.592: ERROR/AndroidRuntime(901):     at es.upv.pros.androiddemos.MainActivity$4.onClick(MainActivity.java:98)
01-21 12:02:17.592: ERROR/AndroidRuntime(901):     at android.view.View.performClick(View.java:2344)
01-21 12:02:17.592: ERROR/AndroidRuntime(901):     at android.view.View.onTouchEvent(View.java:4133)



The myServiceConnection variable is a private local variable in the activity class.

Has somebody any idea of the error?

Thank you so much for helping
pmunoz
Freshman
Freshman
 
Posts: 6
Joined: Tue Apr 13, 2010 4:40 pm

Top

Re: Problem stopping a local service.

Postby pmunoz » Mon Jan 24, 2011 12:20 pm

Hi, the problem were that I did the unbind in the myContext object.
Code: Select all
Context.bindService(intentMessage,myServiceConnection,Context.BIND_AUTO_CREATE);


However, I pretended to do the unbind in another object.
Code: Select all
myService.unbindService(myServiceConnection);


Big error. However, I have a problem yet.


I stopped the service. In detail by means of:

Code: Select all
myContext.unbindService(myServiceConnection);
myContext.stopService(intentMessage);
myService.stopSelf();


However, after that, I try to execute a method of the service.
Code: Select all
connectionResult=myService.getTheValue();


and it works, I don't understand why it works because I stopped the service.


Has somebody any idea?
Thanks.
pmunoz
Freshman
Freshman
 
Posts: 6
Joined: Tue Apr 13, 2010 4:40 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests