1 /* 2 * Copyright (C) 2006 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 android.content; 18 19 import android.os.IBinder; 20 21 /** 22 * Interface for monitoring the state of an application service. See 23 * {@link android.app.Service} and 24 * {@link Context#bindService Context.bindService()} for more information. 25 * <p>Like many callbacks from the system, the methods on this class are called 26 * from the main thread of your process. 27 */ 28 public interface ServiceConnection { 29 /** 30 * Called when a connection to the Service has been established, with 31 * the {@link android.os.IBinder} of the communication channel to the 32 * Service. 33 * 34 * <p class="note"><b>Note:</b> If the system has started to bind your 35 * client app to a service, it's possible that your app will never receive 36 * this callback. Your app won't receive a callback if there's an issue with 37 * the service, such as the service crashing while being created. 38 * 39 * @param name The concrete component name of the service that has 40 * been connected. 41 * 42 * @param service The IBinder of the Service's communication channel, 43 * which you can now make calls on. 44 */ onServiceConnected(ComponentName name, IBinder service)45 void onServiceConnected(ComponentName name, IBinder service); 46 47 /** 48 * Called when a connection to the Service has been lost. This typically 49 * happens when the process hosting the service has crashed or been killed. 50 * This does <em>not</em> remove the ServiceConnection itself -- this 51 * binding to the service will remain active, and you will receive a call 52 * to {@link #onServiceConnected} when the Service is next running. 53 * 54 * @param name The concrete component name of the service whose 55 * connection has been lost. 56 */ onServiceDisconnected(ComponentName name)57 void onServiceDisconnected(ComponentName name); 58 59 /** 60 * Called when the binding to this connection is dead. This means the 61 * interface will never receive another connection. The application will 62 * need to unbind and rebind the connection to activate it again. This may 63 * happen, for example, if the application hosting the service it is bound to 64 * has been updated. 65 * 66 * <p class="note"><b>Note:</b> The app that requested the binding must call 67 * {@link Context#unbindService(ServiceConnection)} to release the tracking 68 * resources associated with this ServiceConnection even if this callback was 69 * invoked following {@link Context#bindService Context.bindService() bindService()}. 70 * 71 * @param name The concrete component name of the service whose connection is dead. 72 */ onBindingDied(ComponentName name)73 default void onBindingDied(ComponentName name) { 74 } 75 76 /** 77 * Called when the service being bound has returned {@code null} from its 78 * {@link android.app.Service#onBind(Intent) onBind()} method. This indicates 79 * that the attempted service binding represented by this ServiceConnection 80 * will never become usable. 81 * 82 * <p class="note"><b>Note:</b> The app that requested the binding must still call 83 * {@link Context#unbindService(ServiceConnection)} to release the tracking 84 * resources associated with this ServiceConnection even if this callback was 85 * invoked following {@link Context#bindService Context.bindService() bindService()}. 86 * 87 * @param name The concrete component name of the service whose binding 88 * has been rejected by the Service implementation. 89 */ onNullBinding(ComponentName name)90 default void onNullBinding(ComponentName name) { 91 } 92 } 93