Home Activity can still call unbind service method. Is it normal?
Reply: 0

Activity can still call unbind service method. Is it normal?

user4101
1#
user4101 Published in June 20, 2018, 10:56 pm

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309361 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO