• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
34 /**
35  * <p>Example of binding and unbinding to the {@link LocalService}.
36  * This demonstrates the implementation of a service which the client will
37  * bind to, receiving an object through which it can communicate with the service.</p>
38  */
39 public class LocalServiceBinding extends Activity {
40     private boolean mIsBound;
41     private LocalService mBoundService;
42 
43     @Override
onCreate(Bundle savedInstanceState)44 	protected void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46 
47         setContentView(R.layout.local_service_binding);
48 
49         // Watch for button clicks.
50         Button button = (Button)findViewById(R.id.bind);
51         button.setOnClickListener(mBindListener);
52         button = (Button)findViewById(R.id.unbind);
53         button.setOnClickListener(mUnbindListener);
54     }
55 
56     private ServiceConnection mConnection = new ServiceConnection() {
57         public void onServiceConnected(ComponentName className, IBinder service) {
58             // This is called when the connection with the service has been
59             // established, giving us the service object we can use to
60             // interact with the service.  Because we have bound to a explicit
61             // service that we know is running in our own process, we can
62             // cast its IBinder to a concrete class and directly access it.
63             mBoundService = ((LocalService.LocalBinder)service).getService();
64 
65             // Tell the user about this for our demo.
66             Toast.makeText(LocalServiceBinding.this, R.string.local_service_connected,
67                     Toast.LENGTH_SHORT).show();
68         }
69 
70         public void onServiceDisconnected(ComponentName className) {
71             // This is called when the connection with the service has been
72             // unexpectedly disconnected -- that is, its process crashed.
73             // Because it is running in our same process, we should never
74             // see this happen.
75             mBoundService = null;
76             Toast.makeText(LocalServiceBinding.this, R.string.local_service_disconnected,
77                     Toast.LENGTH_SHORT).show();
78         }
79     };
80 
81     private OnClickListener mBindListener = new OnClickListener() {
82         public void onClick(View v) {
83             // Establish a connection with the service.  We use an explicit
84             // class name because we want a specific service implementation that
85             // we know will be running in our own process (and thus won't be
86             // supporting component replacement by other applications).
87             bindService(new Intent(LocalServiceBinding.this,
88                     LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
89             mIsBound = true;
90         }
91     };
92 
93     private OnClickListener mUnbindListener = new OnClickListener() {
94         public void onClick(View v) {
95             if (mIsBound) {
96                 // Detach our existing connection.
97                 unbindService(mConnection);
98                 mIsBound = false;
99             }
100         }
101     };
102 }
103 
104