2010-05-28 79 views
2

我正在開發Android上的GPS跟蹤軟件。我需要IPC來控制來自不同活動的服務。所以我決定用AIDL開發遠程服務。這不是一個大問題,但現在它總是運行在接口的方法中,而不是我的服務類中的方法。也許有人可以幫助我?Android遠程服務不調用服務方法

這裏我AIDL文件:

package test.de.android.tracker 
interface ITrackingServiceRemote { 
void startTracking(in long trackId); 
void stopTracking(); 
void pauseTracking(); 
void resumeTracking(in long trackId); 
long trackingState(); 
} 

而且我在這裏的服務類的短版:

public class TrackingService extends Service implements LocationListener{ 
private LocationManager mLocationManager; 
private TrackDb db; 
private long trackId; 
private boolean isTracking = false; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    mNotificationManager = (NotificationManager) this 
      .getSystemService(NOTIFICATION_SERVICE); 
    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    db = new TrackDb(this.getApplicationContext()); 


} 

@Override 
public void onStart(Intent intent, int startId) { 
    super.onStart(intent, startId); 
} 

@Override 
public void onDestroy(){ 
    //TODO 
    super.onDestroy(); 
} 

@Override 
public IBinder onBind(Intent intent){ 
    return this.mBinder; 
} 
private IBinder mBinder = new ITrackingServiceRemote.Stub() { 
    public void startTracking(long trackId) throws RemoteException { 
     TrackingService.this.startTracking(trackId); 
    } 

    public void pauseTracking() throws RemoteException { 
     TrackingService.this.pauseTracking(); 
    } 

    public void resumeTracking(long trackId) throws RemoteException { 
     TrackingService.this.resumeTracking(trackId); 

    } 

    public void stopTracking() throws RemoteException { 
     TrackingService.this.stopTracking(); 
    } 

    public long trackingState() throws RemoteException { 
     long state = TrackingService.this.trackingState(); 
     return state; 
    } 

}; 
public synchronized void startTracking(long trackId) { 
    // request updates every 250 meters or 0 sec 
    this.trackId = trackId; 
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
      0, 250, this); 
    isTracking = true; 
} 

public synchronized long trackingState() { 
    if(isTracking){ 
     return trackId; 
    } else 
     return -1; 

} 

public synchronized void stopTracking() { 
    if(isTracking){ 
     mLocationManager.removeUpdates(this); 
     isTracking = false; 
    } else 
     Log.i(TAG, "Could not stop because service is not tracking at the moment"); 


} 

public synchronized void resumeTracking(long trackId) { 
    if(!isTracking){ 
     this.trackId = trackId; 
     mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       0, 250, this); 
     isTracking = true; 
    } else 
     Log.i(TAG, "Could not resume because service is tracking already track " + this.trackId); 

} 

public synchronized void pauseTracking() { 
    if(isTracking){ 
     mLocationManager.removeUpdates(this); 
     isTracking = false; 
    } else 
     Log.i(TAG, "Could not pause because service is not tracking at the moment"); 

} 
public void onLocationChanged(Location location) { 
//TODO 
} 

對於來自我寫了一個的ServiceManager類樹立ServiceConnection客戶更容易獲得你可以調用服務方法。在這裏我的代碼爲:

public class TrackingServiceManager{ 

private static final String TAG = "TrackingServiceManager"; 
private ITrackingServiceRemote mService = null; 
private Context mContext; 
private Boolean isBound = false; 
private ServiceConnection mServiceConnection; 

public TrackingServiceManager(Context ctx){ 
    this.mContext = ctx; 
} 

public void start(long trackId) { 
    if (isBound && mService != null) { 
     try { 
      mService.startTracking(trackId); 
     } catch (RemoteException e) { 
      Log.e(TAG, "Could not start tracking!",e); 
     } 
    } else 
     Log.i(TAG, "No Service bound! 1"); 
} 

public void stop(){ 
    if (isBound && mService != null) { 
     try { 
      mService.stopTracking(); 
     } catch (RemoteException e) { 
      Log.e(TAG, "Could not stop tracking!",e); 
     } 
    } else 
     Log.i(TAG, "No Service bound!"); 
} 

public void pause(){ 
    if (isBound && mService != null) { 
     try { 
      mService.pauseTracking(); 
     } catch (RemoteException e) { 
      Log.e(TAG, "Could not pause tracking!",e); 
     } 
    } else 
     Log.i(TAG, "No Service bound!"); 
} 

public void resume(long trackId){ 
    if (isBound && mService != null) { 
     try { 
      mService.resumeTracking(trackId); 
     } catch (RemoteException e) { 
      Log.e(TAG, "Could not resume tracking!",e); 
     } 
    } else 
     Log.i(TAG, "No Service bound!"); 
} 

public float state(){ 
    if (isBound && mService != null) { 
     try { 
      return mService.trackingState(); 
     } catch (RemoteException e) { 
      Log.e(TAG, "Could not resume tracking!",e); 
      return -1; 
     } 
    } else 
     Log.i(TAG, "No Service bound!"); 
     return -1; 
} 


/** 
* Method for binding the Service with client 
*/ 
public boolean connectService(){ 

    mServiceConnection = new ServiceConnection() { 
     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      TrackingServiceManager.this.mService = ITrackingServiceRemote.Stub.asInterface(service); 
      } 

     } 

     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      if (mService != null) { 
       mService = null; 
      } 
     } 
    }; 

     Intent mIntent = new Intent("test.de.android.tracker.action.intent.TrackingService"); 
     this.isBound = this.mContext.bindService(mIntent, mServiceConnection, Context.BIND_AUTO_CREATE); 
     return this.isBound; 
} 

public void disconnectService(){ 
     this.mContext.unbindService(mServiceConnection); 
     this.isBound = false; 
} 
} 

如果我現在嘗試調用一個活動的方法,例如start(trackId),則不會發生任何事情。綁定是可以的。調試時,它總是運行到生成的ITrackingServiceRemote.java文件中的startTracking()中,而不是放入我的TrackingService類中。哪裏有問題?我找不到任何錯誤。

在此先感謝!

托比亞斯

回答

2

我需要IPC從 不同的活動控制服務。所以我決定 用AIDL開發一個遠程服務。

您不需要IPC來控制來自不同活動的服務。您可能需要IPC來控制來自不同應用程序(即單獨的APK)的服務。

調試時它總是跑進 startTracking()在生成 ITrackingServiceRemote.java文件和 不進我的TrackingService類。

您的活動有代表服務接口的客戶端代理。服務本身應該與完全獨立的APK完全獨立運行。

我建議您擺脫AIDL並切換回本地綁定模式,至少足夠長的時間以使您的活動和服務工作。然後,只有這樣,如果這確實是您想要的結果,您是否應該將它們拆分爲單獨的APK。