9
Android Service A service is a component that is used to perform operations on the background such as playing music, handle network transactions, interacting content providers etc. It doesn't has any UI (user interface). The service runs in the background indefinitely even if application is destroyed. Moreover, service can be bounded by a component to perform interactivity and inter process communication (IPC). Understanding the Life Cycle of Service There can be two forms of a service. 1. Started 2. Bound The lifecycle of service can follow two different paths: started or bound. 1) Started Service A service is started when component (like activity) calls startService() method, now it runs in the background indefinitely. It is stopped by stopService() method. The service can stop itself by calling the stopSelf() method. 2) Bound Service A service is bound when another component (e.g. client) calls bindService() method. The client can unbind the service by calling the unbindService() method. The service cannot be stopped until all clients unbind the service.

Android Service

Embed Size (px)

DESCRIPTION

tutorial for android service

Citation preview

Android Service A service is a component that is used to perform operations on the background such as playing music, handle network transactions, interacting content providers etc. It doesn't has any UI (user interface).The service runs in the background indefinitely even if application is destroyed.Moreover, service can be bounded by a component to perform interactivity and inter process communication (IPC). Understanding the Life Cycle of ServiceThere can be two forms of a service. 1. Started2. BoundThe lifecycle of service can follow two different paths: started or bound.1) Started ServiceA service is started when component (like activity) calls startService() method, now it runs in the background indefinitely. It is stopped by stopService() method. The service can stop itself by calling the stopSelf() method.2) Bound ServiceA service is bound when another component (e.g. client) calls bindService() method. The client can unbind the service by calling the unbindService() method.The service cannot be stopped until all clients unbind the service.

Understanding Started and Bound Service by background music exampleSuppose, I want to play music in the background, so call startService() method. But I want to get information of the current song being played, I will bind the service that provides information about the current song.Android Service ExampleLet's see the example of service in android that plays an audio in the background. Audio will not be stopped even if you switch to another activity. To stop the audio, you need to stop the service.activity_main.xmlDrag the 3 buttons from the pallete, now the activity_main.xml will look like this:File: activity_main.xml1. 10. 11. 19. 20. 28. 29. 36. 37.

activity_next.xmlIt is the layout file of next activity.File: activity_next.xmlIt contains only one textview displaying the message Next Page1. 10. 11. 20. 21.

Service classNow create the service implemenation class by inheriting the Service class and overridding its callback methods.File: MyService.java1. packagecom.example.serviceexampleaudio;2. 3. importandroid.app.Service;4. importandroid.content.Intent;5. importandroid.media.MediaPlayer;6. importandroid.os.IBinder;7. importandroid.widget.Toast;8. 9. publicclassMyServiceextendsService{10. MediaPlayermyPlayer;11. 12. @Override13. publicIBinderonBind(Intentintent){14. returnnull;15. }16. 17. @Override18. publicvoidonCreate(){19. Toast.makeText(this,"ServiceCreated",Toast.LENGTH_LONG).show();20. 21. myPlayer=MediaPlayer.create(this,R.raw.sun);22. myPlayer.setLooping(false);//Setlooping23. }24. 25. @Override26. publicvoidonStart(Intentintent,intstartid){27. Toast.makeText(this,"ServiceStarted",Toast.LENGTH_LONG).show();28. myPlayer.start();29. }30. 31. @Override32. publicvoidonDestroy(){33. Toast.makeText(this,"ServiceStopped",Toast.LENGTH_LONG).show();34. myPlayer.stop();35. }36. }

Activity classNow create the MainActivity class to perform event handling. Here, we are writing the code to start and stop service. Additionally, calling the second activity on buttonNext.File: MainActivity.java1. packagecom.example.serviceexampleaudio;2. 3. importandroid.app.Activity;4. importandroid.content.Intent;5. importandroid.os.Bundle;6. importandroid.view.View;7. importandroid.view.View.OnClickListener;8. importandroid.widget.Button;9. 10. publicclassMainActivityextendsActivityimplementsOnClickListener{11. ButtonbuttonStart,buttonStop,buttonNext;12. 13. @Override14. publicvoidonCreate(BundlesavedInstanceState){15. super.onCreate(savedInstanceState);16. setContentView(R.layout.activity_main);17. 18. buttonStart=(Button)findViewById(R.id.buttonStart);19. buttonStop=(Button)findViewById(R.id.buttonStop);20. buttonNext=(Button)findViewById(R.id.buttonNext);21. 22. buttonStart.setOnClickListener(this);23. buttonStop.setOnClickListener(this);24. buttonNext.setOnClickListener(this);25. }26. 27. publicvoidonClick(Viewsrc){28. switch(src.getId()){29. caseR.id.buttonStart:30. startService(newIntent(this,MyService.class));31. break;32. caseR.id.buttonStop:33. stopService(newIntent(this,MyService.class));34. break;35. caseR.id.buttonNext:36. Intentintent=newIntent(this,NextPage.class);37. startActivity(intent);38. break;39. }40. }41. }

NextPage classNow, create another activity.File: NextPage.java1. packagecom.example.serviceexampleaudio;2. importandroid.app.Activity;3. importandroid.os.Bundle;4. 5. publicclassNextPageextendsActivity{6. @Override7. publicvoidonCreate(BundlesavedInstanceState){8. super.onCreate(savedInstanceState);9. setContentView(R.layout.activity_next);10. 11. }12. }

Declare the Service in the AndroidManifest.xml file Finally, declare the service in the manifest file.File: AndroidManifest.xmlLet's see the complete AndroidManifest.xml file1. 2. 6. 7. 10. 11. 16. 19. 20. 21. 22. 23. 24. 25. 26. 29. 31. 32. 33.