Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.app;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.app.Activity;
     22 import android.content.ComponentName;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.ServiceConnection;
     26 import android.os.Bundle;
     27 import android.os.IBinder;
     28 import android.view.View;
     29 import android.view.View.OnClickListener;
     30 import android.widget.Button;
     31 import android.widget.Toast;
     32 
     33 public class LocalServiceActivities {
     34     /**
     35      * <p>Example of explicitly starting and stopping the local service.
     36      * This demonstrates the implementation of a service that runs in the same
     37      * process as the rest of the application, which is explicitly started and stopped
     38      * as desired.</p>
     39      *
     40      * <p>Note that this is implemented as an inner class only keep the sample
     41      * all together; typically this code would appear in some separate class.
     42      */
     43     public static class Controller extends Activity {
     44         @Override
     45         protected void onCreate(Bundle savedInstanceState) {
     46             super.onCreate(savedInstanceState);
     47 
     48             setContentView(R.layout.local_service_controller);
     49 
     50             // Watch for button clicks.
     51             Button button = (Button)findViewById(R.id.start);
     52             button.setOnClickListener(mStartListener);
     53             button = (Button)findViewById(R.id.stop);
     54             button.setOnClickListener(mStopListener);
     55         }
     56 
     57         private OnClickListener mStartListener = new OnClickListener() {
     58             public void onClick(View v) {
     59                 // Make sure the service is started.  It will continue running
     60                 // until someone calls stopService().  The Intent we use to find
     61                 // the service explicitly specifies our service component, because
     62                 // we want it running in our own process and don't want other
     63                 // applications to replace it.
     64                 startService(new Intent(Controller.this,
     65                         LocalService.class));
     66             }
     67         };
     68 
     69         private OnClickListener mStopListener = new OnClickListener() {
     70             public void onClick(View v) {
     71                 // Cancel a previous call to startService().  Note that the
     72                 // service will not actually stop at this point if there are
     73                 // still bound clients.
     74                 stopService(new Intent(Controller.this,
     75                         LocalService.class));
     76             }
     77         };
     78     }
     79 
     80     // ----------------------------------------------------------------------
     81 
     82     /**
     83      * Example of binding and unbinding to the local service.
     84      * This demonstrates the implementation of a service which the client will
     85      * bind to, receiving an object through which it can communicate with the service.</p>
     86      *
     87      * <p>Note that this is implemented as an inner class only keep the sample
     88      * all together; typically this code would appear in some separate class.
     89      */
     90     public static class Binding extends Activity {
     91         private boolean mIsBound;
     92 
     93 // BEGIN_INCLUDE(bind)
     94         private LocalService mBoundService;
     95 
     96         private ServiceConnection mConnection = new ServiceConnection() {
     97             public void onServiceConnected(ComponentName className, IBinder service) {
     98                 // This is called when the connection with the service has been
     99                 // established, giving us the service object we can use to
    100                 // interact with the service.  Because we have bound to a explicit
    101                 // service that we know is running in our own process, we can
    102                 // cast its IBinder to a concrete class and directly access it.
    103                 mBoundService = ((LocalService.LocalBinder)service).getService();
    104 
    105                 // Tell the user about this for our demo.
    106                 Toast.makeText(Binding.this, R.string.local_service_connected,
    107                         Toast.LENGTH_SHORT).show();
    108             }
    109 
    110             public void onServiceDisconnected(ComponentName className) {
    111                 // This is called when the connection with the service has been
    112                 // unexpectedly disconnected -- that is, its process crashed.
    113                 // Because it is running in our same process, we should never
    114                 // see this happen.
    115                 mBoundService = null;
    116                 Toast.makeText(Binding.this, R.string.local_service_disconnected,
    117                         Toast.LENGTH_SHORT).show();
    118             }
    119         };
    120 
    121         void doBindService() {
    122             // Establish a connection with the service.  We use an explicit
    123             // class name because we want a specific service implementation that
    124             // we know will be running in our own process (and thus won't be
    125             // supporting component replacement by other applications).
    126             bindService(new Intent(Binding.this,
    127                     LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
    128             mIsBound = true;
    129         }
    130 
    131         void doUnbindService() {
    132             if (mIsBound) {
    133                 // Detach our existing connection.
    134                 unbindService(mConnection);
    135                 mIsBound = false;
    136             }
    137         }
    138 
    139         @Override
    140         protected void onDestroy() {
    141             super.onDestroy();
    142             doUnbindService();
    143         }
    144 // END_INCLUDE(bind)
    145 
    146         private OnClickListener mBindListener = new OnClickListener() {
    147             public void onClick(View v) {
    148                 doBindService();
    149             }
    150         };
    151 
    152         private OnClickListener mUnbindListener = new OnClickListener() {
    153             public void onClick(View v) {
    154                 doUnbindService();
    155             }
    156         };
    157 
    158         @Override
    159         protected void onCreate(Bundle savedInstanceState) {
    160             super.onCreate(savedInstanceState);
    161 
    162             setContentView(R.layout.local_service_binding);
    163 
    164             // Watch for button clicks.
    165             Button button = (Button)findViewById(R.id.bind);
    166             button.setOnClickListener(mBindListener);
    167             button = (Button)findViewById(R.id.unbind);
    168             button.setOnClickListener(mUnbindListener);
    169         }
    170     }
    171 }
    172