Home | History | Annotate | Download | only in app
      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
     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
     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
     91     public IBinder onBind(Intent intent) {
     92         return mBinder;
     93     }
     94 
     95     /**
     96      * Show a notification while this service is running.
     97      */
     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         // Set the icon, scrolling text and timestamp
    103         Notification notification = new Notification(R.drawable.stat_sample, text,
    104                 System.currentTimeMillis());
    105 
    106         // The PendingIntent to launch our activity if the user selects this notification
    107         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
    108                 new Intent(this, AlarmService.class), 0);
    109 
    110         // Set the info for the views that show in the notification panel.
    111         notification.setLatestEventInfo(this, getText(R.string.alarm_service_label),
    112                        text, contentIntent);
    113 
    114         // Send the notification.
    115         // We use a layout id because it is a unique number.  We use it later to cancel.
    116         mNM.notify(R.string.alarm_service_started, notification);
    117     }
    118 
    119     /**
    120      * This is the object that receives interactions from clients.  See RemoteService
    121      * for a more complete example.
    122      */
    123     private final IBinder mBinder = new Binder() {
    124         @Override
    125 		protected boolean onTransact(int code, Parcel data, Parcel reply,
    126 		        int flags) throws RemoteException {
    127             return super.onTransact(code, data, reply, flags);
    128         }
    129     };
    130 }
    131 
    132