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 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.os.PowerManager;
27 import android.util.Log;
28 import android.widget.Toast;
29 
30 // Need the following import to get access to the app resources, since this
31 // class is in a sub-package.
32 import com.example.android.apis.R;
33 
34 /**
35  * This is an example of implementing an application service that runs locally
36  * in the same process as the application.  The {@link LocalServiceActivities.Controller}
37  * and {@link LocalServiceActivities.Binding} classes show how to interact with the
38  * service.
39  *
40  * <p>Notice the use of the {@link NotificationManager} when interesting things
41  * happen in the service.  This is generally how background services should
42  * interact with the user, rather than doing something more disruptive such as
43  * calling startActivity().
44  */
45 //BEGIN_INCLUDE(service)
46 public class LocalService extends Service {
47     private NotificationManager mNM;
48 
49     // Unique Identification Number for the Notification.
50     // We use it on Notification start, and to cancel it.
51     private int NOTIFICATION = R.string.local_service_started;
52 
53     /**
54      * Class for clients to access.  Because we know this service always
55      * runs in the same process as its clients, we don't need to deal with
56      * IPC.
57      */
58     public class LocalBinder extends Binder {
getService()59         LocalService getService() {
60             return LocalService.this;
61         }
62     }
63 
64     @Override
onCreate()65     public void onCreate() {
66         mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
67 
68         // Display a notification about us starting.  We put an icon in the status bar.
69         showNotification();
70     }
71 
72     @Override
onStartCommand(Intent intent, int flags, int startId)73     public int onStartCommand(Intent intent, int flags, int startId) {
74         Log.i("LocalService", "Received start id " + startId + ": " + intent);
75         return START_NOT_STICKY;
76     }
77 
78     @Override
onDestroy()79     public void onDestroy() {
80         // Cancel the persistent notification.
81         mNM.cancel(NOTIFICATION);
82 
83         // Tell the user we stopped.
84         Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
85     }
86 
87     @Override
onBind(Intent intent)88     public IBinder onBind(Intent intent) {
89         return mBinder;
90     }
91 
92     // This is the object that receives interactions from clients.  See
93     // RemoteService for a more complete example.
94     private final IBinder mBinder = new LocalBinder();
95 
96     /**
97      * Show a notification while this service is running.
98      */
showNotification()99     private void showNotification() {
100         // In this sample, we'll use the same text for the ticker and the expanded notification
101         CharSequence text = getText(R.string.local_service_started);
102 
103         // The PendingIntent to launch our activity if the user selects this notification
104         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
105                 new Intent(this, LocalServiceActivities.Controller.class), 0);
106 
107         // Set the info for the views that show in the notification panel.
108         Notification notification = new Notification.Builder(this)
109                 .setSmallIcon(R.drawable.stat_sample)  // the status icon
110                 .setTicker(text)  // the status text
111                 .setWhen(System.currentTimeMillis())  // the time stamp
112                 .setContentTitle(getText(R.string.local_service_label))  // the label of the entry
113                 .setContentText(text)  // the contents of the entry
114                 .setContentIntent(contentIntent)  // The intent to send when the entry is clicked
115                 .build();
116 
117         // Send the notification.
118         mNM.notify(NOTIFICATION, notification);
119     }
120 }
121 //END_INCLUDE(service)
122