Skip to content

Instantly share code, notes, and snippets.

@marshallshen
Created January 10, 2016 18:48
Show Gist options
  • Save marshallshen/ffd8c5e53bb5f1b832e0 to your computer and use it in GitHub Desktop.
Save marshallshen/ffd8c5e53bb5f1b832e0 to your computer and use it in GitHub Desktop.
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