2015-10-22 53 views
0

我将跟踪服务背景以及MainActivity放在同一个包中时我没有收到任何错误,但是当我试图在不同的包中分离它们时(trackingService - com.bustracker.trackingService)和(MainActivity in com.bustracker.monitoring)然后我在这条线mService = binder.getService();下面的错误The method getService() from the type TrackingService.LocalBinder is not visibleTrackingService.LocalBinder类型的方法getService()不可见

我该如何解决它?

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; 
     } 
    } 

} 

回答

3

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

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

相关问题