来自TrackingService.LocalBinder类型的方法getService()不可见

时间:2015-10-22 15:24:15

标签: android service

当我将它们放在同一个包中时,我有跟踪服务背景和MainActivity我没有收到任何错误但是当我尝试将它们分隔在不同的包中时(trackingService - com.bustracker.trackingService)和(MainActivity in com.bustracker.monitoring)然后我得到这一行mService = binder.getService();以下错误The method getService() from the type TrackingService.LocalBinder is not visible

我该如何解决?

MainActivity:

package com.bustracker.monitoring;
public class MainActivity extends ActionBarActivity implements
        AsyncTaskCallback {
    TrackingService mService;
    TrackingService mService;
    boolean mBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.route_available);
        // Start the TrackingService class.
        Intent i = new Intent(this, TrackingService.class);
        startService(i);

        getAvialableRoutes();
        System.out.println("ABC MainActivity onCreate() ");

    }
    private ServiceConnection mConnection = new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
              System.out.println("ABC MainActivity onServiceConnected()");  
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
           //Here I am getting the error "The method getService() from the type TrackingService.LocalBinder is not visible"
            mService = binder.getService();
            mBound = true;          
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
              System.out.println("ABC MainActivity onServiceDisconnected()");   
            mBound = false;         
        }
    };

}

TrackingService:

package com.bustracker.trackingService;
public class TrackingService extends Service implements AsyncTaskCallback,
        LocationListener {
 private final IBinder mBinder = new LocalBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        detectLocation();
        return START_NOT_STICKY;
    }
    public class LocalBinder extends Binder {
        TrackingService getService() {
            // Return this instance of TrackingService so clients can call public methods
            return TrackingService.this;
        }
    }

}

1 个答案:

答案 0 :(得分:3)

  

TrackingService.LocalBinder类型的getService()方法是   不可见

因为getService()方法不是public。在public中将其声明为LocalBinder,以使用binder对象访问它。