Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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