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 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     // Unique Identification Number for the Notification.
     49     // We use it on Notification start, and to cancel it.
     50     private int NOTIFICATION = R.string.local_service_started;
     51 
     52     /**
     53      * Class for clients to access.  Because we know this service always
     54      * runs in the same process as its clients, we don't need to deal with
     55      * IPC.
     56      */
     57     public class LocalBinder extends Binder {
     58         LocalService getService() {
     59             return LocalService.this;
     60         }
     61     }
     62 
     63     @Override
     64     public void onCreate() {
     65         mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
     66 
     67         // Display a notification about us starting.  We put an icon in the status bar.
     68         showNotification();
     69     }
     70 
     71     @Override
     72     public int onStartCommand(Intent intent, int flags, int startId) {
     73         Log.i("LocalService", "Received start id " + startId + ": " + intent);
     74         // We want this service to continue running until it is explicitly
     75         // stopped, so return sticky.
     76         return START_STICKY;
     77     }
     78 
     79     @Override
     80     public void onDestroy() {
     81         // Cancel the persistent notification.
     82         mNM.cancel(NOTIFICATION);
     83 
     84         // Tell the user we stopped.
     85         Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
     86     }
     87 
     88     @Override
     89     public IBinder onBind(Intent intent) {
     90         return mBinder;
     91     }
     92 
     93     // This is the object that receives interactions from clients.  See
     94     // RemoteService for a more complete example.
     95     private final IBinder mBinder = new LocalBinder();
     96 
     97     /**
     98      * Show a notification while this service is running.
     99      */
    100     private void showNotification() {
    101         // In this sample, we'll use the same text for the ticker and the expanded notification
    102         CharSequence text = getText(R.string.local_service_started);
    103 
    104         // Set the icon, scrolling text and timestamp
    105         Notification notification = new Notification(R.drawable.stat_sample, text,
    106                 System.currentTimeMillis());
    107 
    108         // The PendingIntent to launch our activity if the user selects this notification
    109         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
    110                 new Intent(this, LocalServiceActivities.Controller.class), 0);
    111 
    112         // Set the info for the views that show in the notification panel.
    113         notification.setLatestEventInfo(this, getText(R.string.local_service_label),
    114                        text, contentIntent);
    115 
    116         // Send the notification.
    117         mNM.notify(NOTIFICATION, notification);
    118     }
    119 }
    120 //END_INCLUDE(service)
    121