Created
October 18, 2011 07:23
-
-
Save takuo/1294807 to your computer and use it in GitHub Desktop.
Android code snippet - using IntentService
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
public void onClick(View v) { | |
startService(new Intent(this, MyService.class)); | |
} |
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
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