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