Android远程服务不会调用服务方法

时间:2010-05-28 16:48:22

标签: android service aidl

我正在Android上开发GPS跟踪软件。我需要IPC来控制来自不同活动的服务。所以我决定用AIDL开发一个远程服务。这不是一个大问题,但现在它总是遇到接口的方法,而不是我的服务类的方法。也许有人可以帮助我?

这是我的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类。问题出在哪儿?我找不到任何错误。

提前致谢!

托拜厄斯

1 个答案:

答案 0 :(得分:2)

  

我需要IPC来控制服务   不同的活动。所以我决定   使用AIDL开发远程服务。

您不需要IPC来控制来自不同活动的服务。您可能需要IPC来控制来自不同应用程序的服务(即单独的APK)。

  

调试时总会遇到   生成中的startTracking()   ITrackingServiceRemote.java文件和   不要进入我的TrackingService类。

您的活动有一个代表服务接口的客户端代理。服务本身应该在完全独立的APK中完全独立的过程中运行。

我建议您摆脱AIDL并切换回本地绑定模式,至少足以使您的活动和服务正常工作。然后,只有这样,你应该将它们分成单独的APK,如果那确实是理想的结束。