Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Service & handler for internet api


Published on

PT.BUZOO INDONESIA is No1 Japanese offshore development company in Indonesia.
We are professional of web solution and smartphone apps. We can support Japanese, English and Indonesia.
We are hiring now at

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Service & handler for internet api

  1. 1. Buzoo Android Team Lecture Android Service & Handler For Internet API By Niko Yuwono
  2. 2. What is Service? • A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. • A Service is not a separate process. • A Service is not a thread.
  3. 3. Basic Function • onStartCommand() The system calls this method when another component, such as an activity, requests that the service be started, by calling startService(). Once this method executes, the service is started and can run in the background indefinitely.
  4. 4. Basic Function cont’d • onBind() • The system calls this method when another component wants to bind with the service, by calling bindService(). In your implementation of this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder.
  5. 5. Basic Function cont’d • onCreate() • The system calls this method when the service is first created, to perform one-time setup procedures (before it calls either onStartCommand() or onBind()). If the service is already running, this method is not called.
  6. 6. Basic Function cont’d • onDestroy() • The system calls this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc. This is the last call the service receives.
  7. 7. Manifest Declaration <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <service android:name=""/> . . . </application>
  8. 8. Starting and Binding a service • How to start a service • Intent service = new Intent(context, MyService.class); context.startService(service); • How to bind a service • context.bindService(BaseID.iService, scConnection, Context.BIND_AUTO_CREATE);
  9. 9. Stoping and Unbinding a service • To stop and unbind a service it’s very simple just call : • context.unbindService(serviceConnection); • context.stopService(serviceIntent);
  10. 10. What is Handler? • A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue.
  11. 11. Handler Example static class ServiceHandler extends Handler { WeakReference<MyBadgeService> screen; public ServiceHandler(MyBadgeService myScreen) { screen = new WeakReference<MyBadgeService>(myScreen); } @Override public void handleMessage(Message msg) { switch (msg.what) { //HANDLE MESSAGE HERE }
  12. 12. HTTP Access • At First to use the HTTP Access which obviously use internet we shouldn’t forget to add internet permission to manifest -> “android.permission.INTERNET”
  13. 13. HTTP Client • HTTP client is a class that can be used to execute HTTP requests while handling cookies, authentication, connection management, and other features.
  14. 14. HTTP GET and HTTP POST • HTTP GET and POST are class that can be used to construct a GET or POST request from Uri or String that can be executed by HTTP Client. • We can also modify the post/get request header and attach a single entity or even multipart entity with it.
  15. 15. HTTP Response • HTTP Response is a response that sent by the server after HTTP Client executed a request to the server. • The response content will be a stream that also can be streamed one by one : • InputStream in = response.getEntity().getContent(); res = Util.convertStreamToString(in); or directly convert it to string : • EntityUtils.toString(response.getEntity(),"UTF-8");