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.util.Log; 27 import android.os.Bundle; 28 import android.os.IBinder; 29 import android.util.Log; 30 import android.view.View; 31 import android.view.View.OnClickListener; 32 import android.widget.Button; 33 import android.widget.Toast; 34 35 public class LocalServiceActivities { 36 /** 37 * <p>Example of explicitly starting and stopping the local service. 38 * This demonstrates the implementation of a service that runs in the same 39 * process as the rest of the application, which is explicitly started and stopped 40 * as desired.</p> 41 * 42 * <p>Note that this is implemented as an inner class only keep the sample 43 * all together; typically this code would appear in some separate class. 44 */ 45 public static class Controller extends Activity { 46 @Override onCreate(Bundle savedInstanceState)47 protected void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 50 setContentView(R.layout.local_service_controller); 51 52 // Watch for button clicks. 53 Button button = (Button)findViewById(R.id.start); 54 button.setOnClickListener(mStartListener); 55 button = (Button)findViewById(R.id.stop); 56 button.setOnClickListener(mStopListener); 57 } 58 59 private OnClickListener mStartListener = new OnClickListener() { 60 public void onClick(View v) { 61 // Make sure the service is started. It will continue running 62 // until someone calls stopService(). The Intent we use to find 63 // the service explicitly specifies our service component, because 64 // we want it running in our own process and don't want other 65 // applications to replace it. 66 startService(new Intent(Controller.this, 67 LocalService.class)); 68 } 69 }; 70 71 private OnClickListener mStopListener = new OnClickListener() { 72 public void onClick(View v) { 73 // Cancel a previous call to startService(). Note that the 74 // service will not actually stop at this point if there are 75 // still bound clients. 76 stopService(new Intent(Controller.this, 77 LocalService.class)); 78 } 79 }; 80 } 81 82 // ---------------------------------------------------------------------- 83 84 // BEGIN_INCLUDE(bind) 85 /** 86 * Example of binding and unbinding to the local service. 87 * bind to, receiving an object through which it can communicate with the service. 88 * 89 * Note that this is implemented as an inner class only keep the sample 90 * all together; typically this code would appear in some separate class. 91 */ 92 public static class Binding extends Activity { 93 // BEGIN_INCLUDE(bind) 94 // Don't attempt to unbind from the service unless the client has received some 95 // information about the service's state. 96 private boolean mShouldUnbind; 97 98 // To invoke the bound service, first make sure that this value 99 // is not null. 100 private LocalService mBoundService; 101 102 private ServiceConnection mConnection = new ServiceConnection() { 103 public void onServiceConnected(ComponentName className, IBinder service) { 104 // This is called when the connection with the service has been 105 // established, giving us the service object we can use to 106 // interact with the service. Because we have bound to a explicit 107 // service that we know is running in our own process, we can 108 // cast its IBinder to a concrete class and directly access it. 109 mBoundService = ((LocalService.LocalBinder)service).getService(); 110 111 // Tell the user about this for our demo. 112 Toast.makeText(Binding.this, R.string.local_service_connected, 113 Toast.LENGTH_SHORT).show(); 114 } 115 116 public void onServiceDisconnected(ComponentName className) { 117 // This is called when the connection with the service has been 118 // unexpectedly disconnected -- that is, its process crashed. 119 // Because it is running in our same process, we should never 120 // see this happen. 121 mBoundService = null; 122 Toast.makeText(Binding.this, R.string.local_service_disconnected, 123 Toast.LENGTH_SHORT).show(); 124 } 125 }; 126 doBindService()127 void doBindService() { 128 // Attempts to establish a connection with the service. We use an 129 // explicit class name because we want a specific service 130 // implementation that we know will be running in our own process 131 // (and thus won't be supporting component replacement by other 132 // applications). 133 if (bindService(new Intent(Binding.this, LocalService.class), 134 mConnection, Context.BIND_AUTO_CREATE)) { 135 mShouldUnbind = true; 136 } else { 137 Log.e("MY_APP_TAG", "Error: The requested service doesn't " + 138 "exist, or this client isn't allowed access to it."); 139 } 140 } 141 doUnbindService()142 void doUnbindService() { 143 if (mShouldUnbind) { 144 // Release information about the service's state. 145 unbindService(mConnection); 146 mShouldUnbind = false; 147 } 148 } 149 150 @Override onDestroy()151 protected void onDestroy() { 152 super.onDestroy(); 153 doUnbindService(); 154 } 155 // END_INCLUDE(bind) 156 157 private OnClickListener mBindListener = new OnClickListener() { 158 public void onClick(View v) { 159 doBindService(); 160 } 161 }; 162 163 private OnClickListener mUnbindListener = new OnClickListener() { 164 public void onClick(View v) { 165 doUnbindService(); 166 } 167 }; 168 169 @Override onCreate(Bundle savedInstanceState)170 protected void onCreate(Bundle savedInstanceState) { 171 super.onCreate(savedInstanceState); 172 173 setContentView(R.layout.local_service_binding); 174 175 // Watch for button clicks. 176 Button button = (Button)findViewById(R.id.bind); 177 button.setOnClickListener(mBindListener); 178 button = (Button)findViewById(R.id.unbind); 179 button.setOnClickListener(mUnbindListener); 180 } 181 } 182 } 183