2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.altusmetrum.AltosDroid;
19 import org.altusmetrum.AltosDroid.R;
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;
33 public class TelemetryServiceActivities {
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
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.
43 public static class Controller extends Activity {
45 protected void onCreate(Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
48 setContentView(R.layout.telemetry_service_controller);
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);
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 TelemetryService.class));
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 TelemetryService.class));
80 // ----------------------------------------------------------------------
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>
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.
90 public static class Binding extends Activity {
91 private boolean mIsBound;
94 private TelemetryService mBoundService;
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 = ((TelemetryService.TelemetryBinder)service).getService();
105 // Tell the user about this for our demo.
106 Toast.makeText(Binding.this, R.string.telemetry_service_connected,
107 Toast.LENGTH_SHORT).show();
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
115 mBoundService = null;
116 Toast.makeText(Binding.this, R.string.telemetry_service_disconnected,
117 Toast.LENGTH_SHORT).show();
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 TelemetryService.class), mConnection, Context.BIND_AUTO_CREATE);
131 void doUnbindService() {
133 // Detach our existing connection.
134 unbindService(mConnection);
140 protected void onDestroy() {
146 private OnClickListener mBindListener = new OnClickListener() {
147 public void onClick(View v) {
152 private OnClickListener mUnbindListener = new OnClickListener() {
153 public void onClick(View v) {
159 protected void onCreate(Bundle savedInstanceState) {
160 super.onCreate(savedInstanceState);
162 setContentView(R.layout.telemetry_service_binding);
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);