2 * Copyright © 2012 Mike Beattie <mike@ethernal.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.AltosDroid;
20 import android.app.Notification;
21 import android.app.NotificationManager;
22 import android.app.PendingIntent;
23 import android.app.Service;
24 import android.content.Intent;
25 import android.os.Binder;
26 import android.os.IBinder;
27 import android.util.Log;
28 import android.widget.Toast;
30 // Need the following import to get access to the app resources, since this
31 // class is in a sub-package.
32 import org.altusmetrum.AltosDroid.R;
36 public class TelemetryService extends Service {
39 * Class for clients to access. Because we know this service always
40 * runs in the same process as its clients, we don't need to deal with
43 public class TelemetryBinder extends Binder {
44 TelemetryService getService() {
45 return TelemetryService.this;
48 // Unique Identification Number for the Notification.
49 // We use it on Notification start, and to cancel it.
50 private int NOTIFICATION = R.string.telemetry_service_label;
51 private NotificationManager mNM;
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, AltosDroid.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() {
90 // Demote us from the foreground, and cancel the persistent notification.
93 // Tell the user we stopped.
94 Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
98 public IBinder onBind(Intent intent) {
102 // This is the object that receives interactions from clients. See
103 // RemoteService for a more complete example.
104 private final IBinder mBinder = new TelemetryBinder();