Copyright (c) 2010, Moca
All rights reserved.
The source code for Moca is licensed under the BSD license as follows:
Redistribution and use in source and binary forms, with or without modification, ...
If you think the Android project sana listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package org.moca.service;
//fromwww.java2s.comimport android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
publicclass ServiceConnector {
privatestaticfinal String TAG = ServiceConnector.class.toString();
ServiceListener<BackgroundUploader> mListener = null;
private BackgroundUploader mUploadService = null;
private ServiceConnection serviceConnection = new ServiceConnection() {
publicvoid onServiceConnected(ComponentName name,
IBinder service) {
Log.i(TAG, "onServiceConnected");
mUploadService = ((BackgroundUploader.LocalBinder)service).getService();
if (mListener != null)
mListener.onConnect(mUploadService);
}
publicvoid onServiceDisconnected(ComponentName name) {
Log.i(TAG, "onServiceDisconnected");
if (mListener != null)
mListener.onDisconnect(mUploadService);
mUploadService = null;
}
};
publicvoid connect(Context c) {
if (mUploadService == null) {
Intent serviceIntent = new Intent(c, BackgroundUploader.class);
c.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
}
publicvoid disconnect(Context c) {
if (mUploadService != null) {
c.unbindService(serviceConnection);
}
}
publicvoid setServiceListener(ServiceListener<BackgroundUploader> listener) {
this.mListener = listener;
if (listener instanceof Context) {
Log.w(TAG, "Provided ServiceListener is a Context. You may be leaking a Context.");
}
}
}