summaryrefslogtreecommitdiffstats
path: root/core/java/android/app/Service.java
diff options
context:
space:
mode:
authorDianne Hackborn <hackbod@google.com>2009-08-17 23:33:56 -0700
committerDianne Hackborn <hackbod@google.com>2009-08-18 13:59:27 -0700
commitd8a43f61680bacf0d4b52a03ff3c7a07307377fc (patch)
tree298808433ea17b6842b87424629fa1869478ed94 /core/java/android/app/Service.java
parent30c0b83490d856c1cd82441c8e2d800a88927237 (diff)
downloadframeworks_base-d8a43f61680bacf0d4b52a03ff3c7a07307377fc.zip
frameworks_base-d8a43f61680bacf0d4b52a03ff3c7a07307377fc.tar.gz
frameworks_base-d8a43f61680bacf0d4b52a03ff3c7a07307377fc.tar.bz2
Fix issue #2047139: Remove Service.setForeground()
This API is becoming seriously abused, so now it is deprecated and has become a no-op. As an alternative, there is now a new API that allows you to make a service be in the foreground but requires providing a persistent notification to go along with this state, allowing the user to know about and control it.
Diffstat (limited to 'core/java/android/app/Service.java')
-rw-r--r--core/java/android/app/Service.java45
1 files changed, 37 insertions, 8 deletions
diff --git a/core/java/android/app/Service.java b/core/java/android/app/Service.java
index d2fb605..ce72e3f 100644
--- a/core/java/android/app/Service.java
+++ b/core/java/android/app/Service.java
@@ -24,6 +24,7 @@ import android.content.Context;
import android.content.res.Configuration;
import android.os.RemoteException;
import android.os.IBinder;
+import android.util.Log;
import java.io.FileDescriptor;
import java.io.PrintWriter;
@@ -304,24 +305,52 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
}
/**
- * Control whether this service is considered to be a foreground service.
+ * @deprecated This is a now a no-op, use
+ * {@link #startForeground(int, Notification)} instead.
+ */
+ @Deprecated
+ public final void setForeground(boolean isForeground) {
+ Log.w(TAG, "setForeground: ignoring old API call on " + getClass().getName());
+ }
+
+ /**
+ * Make this service run in the foreground, supplying the ongoing
+ * notification to be shown to the user while in this state.
* By default services are background, meaning that if the system needs to
* kill them to reclaim more memory (such as to display a large page in a
* web browser), they can be killed without too much harm. You can set this
- * flag if killing your service would be disruptive to the user: such as
+ * flag if killing your service would be disruptive to the user, such as
* if your service is performing background music playback, so the user
* would notice if their music stopped playing.
*
- * @param isForeground Determines whether this service is considered to
- * be foreground (true) or background (false).
+ * @param id The identifier for this notification as per
+ * {@link NotificationManager#notify(int, Notification)
+ * NotificationManager.notify(int, Notification)}.
+ * @param notification The Notification to be displayed.
+ *
+ * @see #stopForeground(boolean)
*/
- public final void setForeground(boolean isForeground) {
- if (mActivityManager == null) {
- return;
+ public final void startForeground(int id, Notification notification) {
+ try {
+ mActivityManager.setServiceForeground(
+ new ComponentName(this, mClassName), mToken, id,
+ notification, true);
+ } catch (RemoteException ex) {
}
+ }
+
+ /**
+ * Remove this service from foreground state, allowing it to be killed if
+ * more memory is needed.
+ * @param keepNotification If true, the notification previously provided
+ * to {@link #startForeground} will remain displayed.
+ * @see #startForeground(int, Notification)
+ */
+ public final void stopForeground(boolean removeNotification) {
try {
mActivityManager.setServiceForeground(
- new ComponentName(this, mClassName), mToken, isForeground);
+ new ComponentName(this, mClassName), mToken, 0, null,
+ removeNotification);
} catch (RemoteException ex) {
}
}