Services for androi

30 227 0
Services  for androi

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Android Course © 2011 University of Science – HCM City . M.Sc. Bui Tan Loc btloc@fit.hcmus.edu.vn Department of Software Engineering, Faculty of Information Technology, University of Science – Ho Chi Minh City, Viet Nam Services Android Course © 2011 University of Science – HCM City . Objectives • After completing this module, you will be able to: • Create, start, control, and interact with services such as started services, and bound services. 2 Android Course © 2011 University of Science – HCM City . Contents • Services • Started services • The started service lifecycle • The start mode • Starting and stopping started services • Bound services • The bound service lifecycle • The bound services skeleton code 3 Android Course © 2011 University of Science – HCM City . Services • A Service is an application component that can perform long-running operations in the background and does not provide a user interface. • Android supports the concept of a service for two reasons: • First, to allow you to implement background tasks easily. • Second, to allow you to do inter-process communication between applications running on the same device. • These two reasons correspond to the two types of services that Android supports: started services and bound services. 4 Android Course © 2011 University of Science – HCM City . Declaring a service in the manifest <?xml version="1.0" encoding="utf-8"?> <manifest …> <application …> <activity …> </activity> <service android:name=".ExampleService"> </service> </application> </manifest> 5 Android Course © 2011 University of Science – HCM City . Started Services • Started services are services that are started via Context.startService(). Once started, these types of services will continue to run until a client calls Context.stopService() on the service or the service itself calls stopSelf(). • Ex: • It monitors sensor data from the device and does analysis, issues alerts if a certain condition is realized. This service might run constantly. • It might download or upload a file over the network. When the operation is done, the service should stop itself. 6 Android Course © 2011 University of Science – HCM City . The started service lifecyle Method Purpose onCreate() Called when the service process is created. onStartCommand() Called each time the service is sent a command via startService(). onDestroy() Called as the service is being shut down. 7 Android Course © 2011 University of Science – HCM City . Started service skeleton code public class ExampleService extends Service { int mStartMode; // indicates how to behave if the service is killed @Override public void onCreate() { // The service is being created } @Override public int onStartCommand(Intent intent, int flags, int startId) { // The service is starting, due to a call to startService() return mStartMode; } @Override public IBinder onBind(Intent intent) { // We don't provide binding, so return null return null; } @Override public void onDestroy() { // The service is no longer used and is being destroyed } } 8 start mode started service form Android Course © 2011 University of Science – HCM City . The start mode • START_NOT_STICKY: • If the system kills the service after onStartCommand() returns, do not recreate the service, unless there are pending intents to deliver. This is the safest option to avoid running your service when not necessary and when your application can simply restart any unfinished jobs. • START_STICKY: • If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand(), but do not redeliver the last intent. Instead, the system calls onStartCommand() with a null intent, unless there were pending intents to start the service, in which case, those intents are delivered. This is suitable for media players (or similar services) that are not executing commands, but running indefinitely and waiting for a job. • START_REDELIVER_INTENT: • If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand() with the last intent that was delivered to the service. Any pending intents are delivered in turn. This is suitable for services that are actively performing a job that should be immediately resumed, such as downloading a file. 9 Android Course © 2011 University of Science – HCM City . Starting & Stopping a started service • Starting a Service: Intent intent = new Intent(this, ExampleService.class); startService(intent); • Stopping a Service: • using stopSelft() methods of Service class • using stopService() methods of Context class (ex: Activity class) Intent intent = new Intent(this, ExampleService.class); stopService(intent); 10 [...]... bound service form? • In which case do we use started services? • In which case do we use bound services? © 2011 University of Science – HCM City Android Course References & Further Readings • Creating a simple started service by extending the IntenService class • http:// developer.android.com/guide/topics/fundamentals /services. html#Exte ndingIntentService • Using a Messenger to perform inter-process... developer.android.com/guide/topics/fundamentals/bound -services. htm l#Messenger © 2011 University of Science – HCM City Android Course References & Further readings • Professional Android 2 Application Development, Reto Meier, Wiley Publishing (2010) • Introducing Services, Chapter 9 © 2011 University of Science – HCM City Android Course References & Further Readings • Beginning Murphy, (2011) Android... Murphy, (2011) Android 3, Mark Apress Publishing • Services: The Theory, Chapter 35 © 2011 University of Science – HCM City Android Course References & Further Readings • Pro Android 3, Mark Murphy, Apress Publishing (2011) • Understanding Android Services, Chapter 11 © 2011 University of Science – HCM City Android Course References & Further Readings • Android™ Wireless Application Development Second... UI HandlerThread thread = new HandlerThread("ServiceStartArguments", android.os.Process.THREAD_PRIORITY_BACKGROUND); thread.start(); // Get the HandlerThread's Looper and use it for our Handler mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } Let service handler do the job © 2011 University of Science – HCM City Android Course StopSelfService example @Override... @Override public IBinder onBind(Intent intent) { return mBinder; Bound service form } /** method for clients */ public int getRandomNumber() { return mGenerator.nextInt(100); } // LocalBinder inner class } © 2011 University of Science – HCM City Android Course BoundService example: LocalBinder inner class /** * Class used for the client Binder Because we know this service * always runs in the same process... performance int num = mService.getRandomNumber(); Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show(); } } © 2011 University of Science – HCM City Android Course More techniques • Creating • a simple started service by extending the IntenService class Using a Messenger to perform inter-process communication (IPC) without the need to use AIDL © 2011 University of Science – HCM City Android... 2011 University of Science – HCM City Android Course StopSelfService example: ServiceHandler inner class private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Override do the job public void handleMessage(Message msg) { // Normally we would do some work here, like download a file // For our sample, we just sleep for 5 seconds long endTime = System.currentTimeMillis()... onBind(Intent intent) { // We don't provide binding, so return null return null; started service form } @Override public void onDestroy() { Toast.makeText(getApplicationContext(), "service done", Toast.LENGTH_SHORT).show(); // Log.i("", "Service done!"); } © 2011 University of Science – HCM City Android Course StopSelfService example @Override public void onCreate() { // Start up... clients can call // public methods return BoundService.this; } } © 2011 University of Science – HCM City Android Course Calling BoundService from an Activity public class BoundServiceDemoActivity extends Activity { private BoundService mService; private boolean mBound = false; /** Defines callbacks for service binding, passed to bindService() */ private ServiceConnection mConnection = new ServiceConnection()... Unbind from the service if (mBound) { unbindService(mConnection); mBound = false; } } © 2011 University of Science – HCM City Android Course Calling BoundService from an Activity /** * Called when a button is clicked (the button in the layout file attaches * to this method with the android:onClick attribute) */ public void onButtonClick(View v) { if (mBound) { // Call a method from the LocalService // . interact with services such as started services, and bound services. 2 Android Course © 2011 University of Science – HCM City . Contents • Services • Started services • The started service lifecycle • The. and stopping started services • Bound services • The bound service lifecycle • The bound services skeleton code 3 Android Course © 2011 University of Science – HCM City . Services • A Service. University of Science – HCM City . Started Services • Started services are services that are started via Context.startService(). Once started, these types of services will continue to run until a

Ngày đăng: 02/02/2015, 11:13

Từ khóa liên quan

Mục lục

  • Slide 1

  • Objectives

  • Contents

  • Services

  • Declaring a service in the manifest

  • Started Services

  • The started service lifecyle

  • Started service skeleton code

  • The start mode

  • Starting & Stopping a started service

  • SimpleService example

  • SimpleService example

  • StopSelfService example

  • StopSelfService example

  • StopSelfService example

  • StopSelfService example: ServiceHandler inner class

  • Bound Services

  • The bound service lifecycle

  • BoundService example

  • BoundService example: LocalBinder inner class

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan