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 android.app; 18 19 import android.content.Context; 20 import android.os.RemoteException; 21 import android.os.Handler; 22 import android.os.IBinder; 23 import android.os.ServiceManager; 24 import android.util.Log; 25 26 /** 27 * Class to notify the user of events that happen. This is how you tell 28 * the user that something has happened in the background. {@more} 29 * 30 * Notifications can take different forms: 31 * <ul> 32 * <li>A persistent icon that goes in the status bar and is accessible 33 * through the launcher, (when the user selects it, a designated Intent 34 * can be launched),</li> 35 * <li>Turning on or flashing LEDs on the device, or</li> 36 * <li>Alerting the user by flashing the backlight, playing a sound, 37 * or vibrating.</li> 38 * </ul> 39 * 40 * <p> 41 * Each of the notify methods takes an int id parameter. This id identifies 42 * this notification from your app to the system, so that id should be unique 43 * within your app. If you call one of the notify methods with an id that is 44 * currently active and a new set of notification parameters, it will be 45 * updated. For example, if you pass a new status bar icon, the old icon in 46 * the status bar will be replaced with the new one. This is also the same 47 * id you pass to the {@link #cancel} method to clear this notification. 48 * 49 * <p> 50 * You do not instantiate this class directly; instead, retrieve it through 51 * {@link android.content.Context#getSystemService}. 52 * 53 * @see android.app.Notification 54 * @see android.content.Context#getSystemService 55 */ 56 public class NotificationManager 57 { 58 private static String TAG = "NotificationManager"; 59 private static boolean DEBUG = false; 60 private static boolean localLOGV = DEBUG || android.util.Config.LOGV; 61 62 private static INotificationManager sService; 63 64 /** @hide */ getService()65 static public INotificationManager getService() 66 { 67 if (sService != null) { 68 return sService; 69 } 70 IBinder b = ServiceManager.getService("notification"); 71 sService = INotificationManager.Stub.asInterface(b); 72 return sService; 73 } 74 NotificationManager(Context context, Handler handler)75 /*package*/ NotificationManager(Context context, Handler handler) 76 { 77 mContext = context; 78 } 79 80 /** 81 * Persistent notification on the status bar, 82 * 83 * @param id An identifier for this notification unique within your 84 * application. 85 * @param notification A {@link Notification} object describing how to 86 * notify the user, other than the view you're providing. Must not be null. 87 */ notify(int id, Notification notification)88 public void notify(int id, Notification notification) 89 { 90 notify(null, id, notification); 91 } 92 93 /** 94 * Persistent notification on the status bar, 95 * 96 * @param tag An string identifier for this notification unique within your 97 * application. 98 * @param notification A {@link Notification} object describing how to 99 * notify the user, other than the view you're providing. Must not be null. 100 * @return the id of the notification that is associated with the string identifier that 101 * can be used to cancel the notification 102 */ notify(String tag, int id, Notification notification)103 public void notify(String tag, int id, Notification notification) 104 { 105 int[] idOut = new int[1]; 106 INotificationManager service = getService(); 107 String pkg = mContext.getPackageName(); 108 if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")"); 109 try { 110 service.enqueueNotificationWithTag(pkg, tag, id, notification, idOut); 111 if (id != idOut[0]) { 112 Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]); 113 } 114 } catch (RemoteException e) { 115 } 116 } 117 118 /** 119 * Cancel a previously shown notification. If it's transient, the view 120 * will be hidden. If it's persistent, it will be removed from the status 121 * bar. 122 */ cancel(int id)123 public void cancel(int id) 124 { 125 cancel(null, id); 126 } 127 128 /** 129 * Cancel a previously shown notification. If it's transient, the view 130 * will be hidden. If it's persistent, it will be removed from the status 131 * bar. 132 */ cancel(String tag, int id)133 public void cancel(String tag, int id) 134 { 135 INotificationManager service = getService(); 136 String pkg = mContext.getPackageName(); 137 if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")"); 138 try { 139 service.cancelNotificationWithTag(pkg, tag, id); 140 } catch (RemoteException e) { 141 } 142 } 143 144 /** 145 * Cancel all previously shown notifications. See {@link #cancel} for the 146 * detailed behavior. 147 */ cancelAll()148 public void cancelAll() 149 { 150 INotificationManager service = getService(); 151 String pkg = mContext.getPackageName(); 152 if (localLOGV) Log.v(TAG, pkg + ": cancelAll()"); 153 try { 154 service.cancelAllNotifications(pkg); 155 } catch (RemoteException e) { 156 } 157 } 158 159 private Context mContext; 160 } 161