Created
January 10, 2016 18:48
-
-
Save marshallshen/ffd8c5e53bb5f1b832e0 to your computer and use it in GitHub Desktop.
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 MainActivity extends Activity { | |
// Method to start the service | |
public void startService(View view) { | |
startService(new Intent(getBaseContext(), MyService.class)); | |
} | |
// Method to stop the service | |
public void stopService(View view) { | |
stopService(new Intent(getBaseContext(), MyService.class)); | |
} | |
} | |
public class MyService extends Service { | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
// Let it continue running until it is stopped. | |
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); | |
return START_STICKY; | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment