Activity can still call unbind service method. Is it normal?
I am studying services, and I have written a code to bind a service by clicking on a button, run a method of the service by clicking on another button and unbinding service by clicking on a third button. If I try to run the method of the service before binding. I get, obviously, an error message, while if I first bind the service the method is normally called. The question is, If I click on the third button to unbind the service, despite the service native method on Unbind(Intent intent) gives me a positive feedback, I'm still able to call the service method from the main activity like if it should still be bound.
Here is the Service code
package com.antonello.tavolaccio4; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; /** * Created by Antonello on 14/05/2017. */ public class Servizio extends Service{ public IBinder binder=new MyBinder(); @Nullable @Override public IBinder onBind(Intent intent) { return binder; } @Override public boolean onUnbind(Intent intent){ System.out.println("unbinded"); return false; } public class MyBinder extends Binder{ Servizio getService(){ return Servizio.this; } } public void metodo(){ System.out.println("Metodo del service"); } }
and here is the Main Activity code:
package com.antonello.tavolaccio4; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { ServiceConnection serviceConnection; Servizio servizio; Button button; Button button2; Button button3; boolean bound; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button); button2=(Button)findViewById(R.id.button2); button3=(Button)findViewById(R.id.button3); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(getApplicationContext(),Servizio.class); bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE); } }); button2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { servizio.metodo(); } }); button3.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Intent intent=new Intent(getApplicationContext(),Servizio.class); unbindService(serviceConnection); } }); serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Servizio.MyBinder binder=(Servizio.MyBinder)service; servizio=binder.getService(); bound=true; System.out.println(bound); } @Override public void onServiceDisconnected(ComponentName name) { } }; } }
Is there anything wrong in my unbindService method? Thank you for any help.
Is there anything wrong in my unbindService method?
You do not have an unbindService()
method. You are calling the one that you are inheriting from Context
.
It is your job, as part of calling unbindService()
, to also set to null
any fields tied to the bound connection (in your case, servizio
). As it stands, you are leaking memory, and any inherited methods that your service tries calling may throw exceptions because the service is destroyed.