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 android.app.Notification;
20 import android.app.NotificationManager;
21 import android.app.PendingIntent;
22 import android.app.Service;
23 import android.content.Intent;
24 import android.os.Binder;
25 import android.os.IBinder;
26 import android.util.Log;
27 import android.widget.Toast;
29 // Need the following import to get access to the app resources, since this
30 // class is in a sub-package.
31 import org.altusmetrum.AltosDroid.R;
35 public class TelemetryService extends Service {
36 private NotificationManager mNM;
38 // Unique Identification Number for the Notification.
39 // We use it on Notification start, and to cancel it.
40 private int NOTIFICATION = R.string.telemetry_service_started;
43 * Class for clients to access. Because we know this service always
44 * runs in the same process as its clients, we don't need to deal with
47 public class TelemetryBinder extends Binder {
48 TelemetryService getService() {
49 return TelemetryService.this;
54 public void onCreate() {
55 mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
57 // Display a notification about us starting. We put an icon in the status bar.
62 public int onStartCommand(Intent intent, int flags, int startId) {
63 Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
64 // We want this service to continue running until it is explicitly
65 // stopped, so return sticky.
70 public void onDestroy() {
71 // Cancel the persistent notification.
72 mNM.cancel(NOTIFICATION);
74 // Tell the user we stopped.
75 Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
79 public IBinder onBind(Intent intent) {
83 // This is the object that receives interactions from clients. See
84 // RemoteService for a more complete example.
85 private final IBinder mBinder = new TelemetryBinder();
88 * Show a notification while this service is running.
90 private void showNotification() {
91 // In this sample, we'll use the same text for the ticker and the expanded notification
92 CharSequence text = getText(R.string.telemetry_service_started);
94 // Set the icon, scrolling text and timestamp
95 Notification notification = new Notification(R.drawable.am_status, text,
96 System.currentTimeMillis());
98 // The PendingIntent to launch our activity if the user selects this notification
99 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
100 new Intent(this, TelemetryServiceActivities.Controller.class), 0);
102 // Set the info for the views that show in the notification panel.
103 notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label),
104 text, contentIntent);
106 // Send the notification.
107 mNM.notify(NOTIFICATION, notification);