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 // Need the following import to get access to the app resources, since this 20 // class is in a sub-package. 21 import com.example.android.apis.R; 22 23 import android.app.Notification; 24 import android.app.NotificationManager; 25 import android.app.PendingIntent; 26 import android.app.Service; 27 import android.content.Intent; 28 import android.os.Binder; 29 import android.os.IBinder; 30 import android.os.Parcel; 31 import android.os.RemoteException; 32 import android.widget.Toast; 33 34 /** 35 * This is an example of implementing an application service that will run in 36 * response to an alarm, allowing us to move long duration work out of an 37 * intent receiver. 38 * 39 * @see AlarmService 40 * @see AlarmService_Alarm 41 */ 42 public class AlarmService_Service extends Service { 43 NotificationManager mNM; 44 45 @Override onCreate()46 public void onCreate() { 47 mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 48 49 // show the icon in the status bar 50 showNotification(); 51 52 // Start up the thread running the service. Note that we create a 53 // separate thread because the service normally runs in the process's 54 // main thread, which we don't want to block. 55 Thread thr = new Thread(null, mTask, "AlarmService_Service"); 56 thr.start(); 57 } 58 59 @Override onDestroy()60 public void onDestroy() { 61 // Cancel the notification -- we use the same ID that we had used to start it 62 mNM.cancel(R.string.alarm_service_started); 63 64 // Tell the user we stopped. 65 Toast.makeText(this, R.string.alarm_service_finished, Toast.LENGTH_SHORT).show(); 66 } 67 68 /** 69 * The function that runs in our worker thread 70 */ 71 Runnable mTask = new Runnable() { 72 public void run() { 73 // Normally we would do some work here... for our sample, we will 74 // just sleep for 30 seconds. 75 long endTime = System.currentTimeMillis() + 15*1000; 76 while (System.currentTimeMillis() < endTime) { 77 synchronized (mBinder) { 78 try { 79 mBinder.wait(endTime - System.currentTimeMillis()); 80 } catch (Exception e) { 81 } 82 } 83 } 84 85 // Done with our work... stop the service! 86 AlarmService_Service.this.stopSelf(); 87 } 88 }; 89 90 @Override onBind(Intent intent)91 public IBinder onBind(Intent intent) { 92 return mBinder; 93 } 94 95 /** 96 * Show a notification while this service is running. 97 */ showNotification()98 private void showNotification() { 99 // In this sample, we'll use the same text for the ticker and the expanded notification 100 CharSequence text = getText(R.string.alarm_service_started); 101 102 // The PendingIntent to launch our activity if the user selects this notification 103 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 104 new Intent(this, AlarmService.class), 0); 105 106 // Set the info for the views that show in the notification panel. 107 Notification notification = new Notification.Builder(this) 108 .setSmallIcon(R.drawable.stat_sample) // the status icon 109 .setTicker(text) // the status text 110 .setWhen(System.currentTimeMillis()) // the time stamp 111 .setContentTitle(getText(R.string.alarm_service_label)) // the label of the entry 112 .setContentText(text) // the contents of the entry 113 .setContentIntent(contentIntent) // The intent to send when the entry is clicked 114 .build(); 115 116 // Send the notification. 117 // We use a layout id because it is a unique number. We use it later to cancel. 118 mNM.notify(R.string.alarm_service_started, notification); 119 } 120 121 /** 122 * This is the object that receives interactions from clients. See RemoteService 123 * for a more complete example. 124 */ 125 private final IBinder mBinder = new Binder() { 126 @Override 127 protected boolean onTransact(int code, Parcel data, Parcel reply, 128 int flags) throws RemoteException { 129 return super.onTransact(code, data, reply, flags); 130 } 131 }; 132 } 133 134