Skip to content

Instantly share code, notes, and snippets.

@shurane
Created June 5, 2012 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shurane/2877718 to your computer and use it in GitHub Desktop.
Save shurane/2877718 to your computer and use it in GitHub Desktop.
android.os.Message being delayed.
### binding part
public Something extends Activity {
public void analyticsBind(){
Log.d(TAG, "bindToAnalyticsService()");
Intent i = new Intent();
i.setClassName("com.gimme.service", "com.gimme.service.SomeService");
bindService(i, mAnalyticsConnection, Context.BIND_AUTO_CREATE);
}
}
### sending part
public Something extends Activity {
//initialize these variables in other methods
boolean mBound = false;
public static final int ANALYTICS_SEND_CLICK = 1;
private Messenger mAnalyticsService = null;
public void analyticsSendClick(String clicktype){
if (!mBound)
return;
Message analyticsMsg = Message.obtain(null, ANALYTICS_SEND_CLICK, 0, 0);
Bundle bundle = analyticsMsg.getData();
bundle.putString(ANALYTICS_CLICKTYPE,clicktype);
analyticsMsg.setData(bundle);
try {
mAnalyticsService.send(analyticsMsg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
### handling part
public SomeService extends Service {
final Messenger mMessenger = new Messenger(new IncomingHandler());
public static final int SEND_TO_SERVER = 0;
public static final int SEND_CLICK = 1;
private class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "handleMessage(): Got a message coming around!");
if (msg.what == SEND_TO_SERVER) {
final String objectAsString = "{\"is_test\":true}";
}
else {
Bundle bundle = msg.getData();
String typeString = bundle.getString(CLICKTYPE);
Log.d(TAG, "Got the following clicktype: " + typeString);
}
}
}
@Override
public IBinder onBind(Intent arg0) {
Log.d(TAG, "onBind()");
return mMessenger.getBinder();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment