Service not being created (or connecting) after bindService()

后端 未结 3 811
南笙
南笙 2021-02-08 11:55

I have this simple service that broadcasts the current location of the user. I want to use the binding mechanism just to control the service life-cycle, but the service is just

3条回答
  •  深忆病人
    2021-02-08 12:27

    The onStartCommand will only execute if the service is explicitly started, it looks like you are just wanting to bind to it, which is fine. I don't see that you have set up the service connection properly though. I'm posting my stub program which shows how to bind to a service and call a method in the service through a binder. You may like to run this and see the sequence of the various log messages. You will obviously need to add your BroadcastReceiver and onLocationChaged code to make it useful for you.

    The Activity

    package com.servtest.test;
    
    import com.servtest.test.LocationService.LocalBinder;
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.util.Log;
    
    public class ServiceTestActivity extends Activity {
    
        boolean mServiceConnected = false;
        boolean mBound = false;
        private LocationService mLocnServ;
    
        ServiceConnection mServconn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d("SVTEST", "Activity service connected");
                LocalBinder binder = (LocalBinder) service;
                mLocnServ = binder.getService();
                // Can't call this methodInTheService UNTIL IT'S BOUND!
                mLocnServ.methodInTheService();
                mBound = true;
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d("SVTEST", "Activity service disconnected");
                mBound = false;
            }
        };
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
        @Override
        public void onStart() {
            super.onStart();
            Log.d("SVTEST", "Activity onStart");
            mServiceConnected = bindService(new Intent(
                    "com.servtest.test.LOCATIONSERVICE"), mServconn,
                    Context.BIND_AUTO_CREATE);
        }
        @Override
        protected void onResume() {
            super.onResume();
            Log.d("SVTEST", "Activity onResume");
        }
        @Override
        public void onPause() {
            Log.d("SVTEST", "Activity onPause");
            super.onPause();
        }
        @Override
        public void onStop() {
            Log.d("SVTEST", "Activity onStop");
            if (mBound) {
                unbindService(mServconn);
                mBound = false;
            }
            super.onStop();
        }
    
    }
    

    The Service

    package com.servtest.test;
    
    import android.app.Service;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationListener;
    import android.os.Binder;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.util.Log;
    
    public class LocationService extends Service implements LocationListener {
    
        private final IBinder mBinder = new LocalBinder();
    
        @Override
        public void onLocationChanged(Location arg0) {}
        @Override
        public void onProviderDisabled(String arg0) {}
        @Override
        public void onProviderEnabled(String arg0) {}
        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
    
        @Override
        public IBinder onBind(Intent intent) {
            Log.d("SVTEST", "Loc service ONBIND");
            return mBinder;
        }
        @Override
        public boolean onUnbind(Intent intent) {
            Log.d("SVTEST", "Loc service ONUNBIND");
            return super.onUnbind(intent);
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // Won't run unless it's EXPLICITLY STARTED
            Log.d("SVTEST", "Loc service ONSTARTCOMMAND");
            return super.onStartCommand(intent, flags, startId);
        }
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.d("SVTEST", "Loc service ONDESTROY");
        }
    
        public class LocalBinder extends Binder {
            LocationService getService() {
                // Return this instance of LocalService so clients can call public methods
                return LocationService.this;
            }
        }
    
        public void methodInTheService() {
            // A method you can call in the service
            Log.d("SVTEST", "Loc service EXECUTING THE METHOD");
        }
    }
    

    The Manifest

    
    
        
            
                
                    
                    
                
            
            
                
                    
                
            
        
    
    

    Hope this helps

提交回复
热议问题