Skip to content

Instantly share code, notes, and snippets.

@takuo
Created October 18, 2011 07:23
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 takuo/1294807 to your computer and use it in GitHub Desktop.
Save takuo/1294807 to your computer and use it in GitHub Desktop.
Android code snippet - using IntentService
public void onClick(View v) {
startService(new Intent(this, MyService.class));
}
public class MyService extends IntentService {
private Context mContext;
private Handler mHandler;
public MyService(String name) {
super(name);
}
// call from startService()
public MyService() {
super("MyService");
mHandler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
mContext = getApplicationContext();
mHandler.post(new Runnable(){
@Override
public void run() {
Toast.makeText(mContext, "Toast Message from IntentService", Toast.LENGTH_LONG).show();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment