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_label;
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 // Create a reference to the NotificationManager so that we can update our notifcation text later
56 mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
60 public int onStartCommand(Intent intent, int flags, int startId) {
61 Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
63 CharSequence text = getText(R.string.telemetry_service_started);
65 // Create notification to be displayed while the service runs
66 Notification notification = new Notification(R.drawable.am_status_c, text, 0);
68 // The PendingIntent to launch our activity if the user selects this notification
69 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
70 new Intent(this, TelemetryServiceActivities.Controller.class), 0);
72 // Set the info for the views that show in the notification panel.
73 notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent);
75 // Set the notification to be in the "Ongoing" section.
76 notification.flags |= Notification.FLAG_ONGOING_EVENT;
78 // Move us into the foreground.
79 startForeground(NOTIFICATION, notification);
81 // We want this service to continue running until it is explicitly
82 // stopped, so return sticky.
87 public void onDestroy() {
88 // Demote us from the foreground, and cancel the persistent notification.
91 // Tell the user we stopped.
92 Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
96 public IBinder onBind(Intent intent) {
100 // This is the object that receives interactions from clients. See
101 // RemoteService for a more complete example.
102 private final IBinder mBinder = new TelemetryBinder();