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