Last active
August 29, 2015 14:20
-
-
Save vsmaks/1fad1332956b1bbb9973 to your computer and use it in GitHub Desktop.
android.content.ServiceConnection implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Service; | |
import android.content.ComponentName; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.ServiceConnection; | |
import android.os.IBinder; | |
import android.os.Message; | |
import android.os.Messenger; | |
import android.os.RemoteException; | |
import android.util.Log; | |
import java.util.Queue; | |
import java.util.concurrent.LinkedBlockingQueue; | |
public class Connection implements ServiceConnection, IServiceConnection | |
{ | |
public static final String TAG = Connection.class.getName(); | |
private Context mContext; | |
private Class< ? extends Service> mServiceClass; | |
private Messenger mServiceMessenger; | |
private boolean mIsBound = false; | |
private Queue<Message> mQueue; | |
public Connection(Context context, Class<? extends Service> service) | |
{ | |
mContext = context; | |
mServiceClass = service; | |
mQueue = new LinkedBlockingQueue<Message>(); | |
} | |
@Override | |
public void onServiceConnected(ComponentName name, IBinder service) | |
{ | |
Log.d(TAG, "onServiceConnected"); | |
mServiceMessenger = new Messenger(service); | |
while(!mQueue.isEmpty()) | |
{ | |
sendMessage(mQueue.poll()); | |
} | |
} | |
@Override | |
public void onServiceDisconnected(ComponentName name) | |
{ | |
Log.d(TAG, "onServiceDisconnected"); | |
mServiceMessenger = null; | |
rebind(); | |
} | |
@Override | |
public void sendMessage(Message msg) | |
{ | |
if (isBound()) | |
{ | |
Log.d(TAG, "Send message"); | |
try | |
{ | |
mServiceMessenger.send(msg); | |
} | |
catch (RemoteException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
else | |
{ | |
Log.d(TAG, "Add to queue"); | |
mQueue.add(msg); | |
bind(); | |
} | |
} | |
@Override | |
public void bind() | |
{ | |
if (!mIsBound) | |
{ | |
Log.d(TAG, "Bind"); | |
mContext.bindService(new Intent(mContext, mServiceClass), this, Context.BIND_AUTO_CREATE); | |
mIsBound = true; | |
} | |
} | |
@Override | |
public void unbind() | |
{ | |
if (mIsBound) | |
{ | |
Log.d(TAG, "Unbind"); | |
mContext.unbindService(this); | |
mIsBound = false; | |
} | |
} | |
@Override | |
public void rebind() | |
{ | |
Log.d(TAG, "Rebind"); | |
unbind(); | |
bind(); | |
} | |
@Override | |
public boolean isBound() | |
{ | |
return mIsBound && mServiceMessenger != null; | |
} | |
@Override | |
public void start() | |
{ | |
Log.d(TAG, "start"); | |
mContext.startService(new Intent(mContext, mServiceClass)); | |
} | |
@Override | |
public void stop() | |
{ | |
Log.d(TAG, "stop"); | |
mContext.stopService(new Intent(mContext, mServiceClass)); | |
} | |
@Override | |
public void restart() | |
{ | |
Log.d(TAG, "restart"); | |
stop(); | |
start(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Message; | |
public interface IServiceConnection | |
{ | |
void bind(); | |
void unbind(); | |
void rebind(); | |
boolean isBound(); | |
void start(); | |
void stop(); | |
void restart(); | |
void sendMessage(Message msg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment