Created
April 6, 2012 22:29
-
-
Save twaddington/2323563 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public classd MyActivity extends Activity implements OnClickListener { | |
private LQService mService; | |
private boolean mBound; | |
private SampleReceiver mLocationReceiver = new SampleReceiver(); | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
// Bind to the tracking service so we can call public methods on it! | |
Intent intent = new Intent(this, LQService.class); | |
bindService(intent, mConnection, 0); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
// Unbind from LQService. This is necessary or the Android | |
// OS will throw a warning to logcat. | |
if (mBound) { | |
unbindService(mConnection); | |
mBound = false; | |
} | |
} | |
@Override | |
public void onClick(View v) { | |
// Make sure the Activity has finished binding to the service | |
// before we try to reference it! | |
if (mBound && mService != null) { | |
LQSession session = mService.getSession(); | |
// Get the current user's profile | |
session.runGetRequest("account/profile", new OnRunApiRequestListener() { | |
@Override | |
public void onSuccess(LQSession session, HttpResponse response) { | |
// Consume the response! | |
// ... | |
JSONObject json = new JSONObject(EntityUtils.toString(response.getEntity())); | |
} | |
@Override | |
public void onFailure(LQSession session, LQException e) { | |
Log.e(TAG, e.getMessage()); | |
} | |
@Override | |
public void onComplete(LQSession session, HttpResponse response, StatusLine status) { | |
// The server did not return a 200-OK response! | |
// ... | |
} | |
}); | |
} | |
} | |
/** | |
* The bindService call in onResume references this service connection. It | |
* is called asynchronously as your Activity is started. Once the Activity | |
* has successfully bound to the service the onServiceConnected callback | |
* is triggered. Once this happens, you can reference mService from | |
* the other Activity lifecycle methods. | |
*/ | |
private ServiceConnection mConnection = new ServiceConnection() { | |
@Override | |
public void onServiceConnected(ComponentName name, IBinder service) { | |
try { | |
// We've bound to LocalService, cast the IBinder and get LocalService instance. | |
LQBinder binder = (LQBinder) service; | |
mService = binder.getService(); | |
mBound = true; | |
// The service is now bound! You can now call public | |
// service methods. | |
} catch (ClassCastException e) { | |
// Pass | |
} | |
} | |
@Override | |
public void onServiceDisconnected(ComponentName name) { | |
mBound = false; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment