diff options
| author | Hui Yu <huiyu@google.com> | 2019-02-20 10:04:07 -0800 |
|---|---|---|
| committer | Hui Yu <huiyu@google.com> | 2019-02-26 12:50:28 -0800 |
| commit | fb72b990511b4546f0e89a1a0c7e44d13d7858dc (patch) | |
| tree | a89166be255add8452d56ef29d0b1befb2aae596 /core/java/android/app/Service.java | |
| parent | c7e26f7fbb87a62fb6a92973543a04e7d901dc28 (diff) | |
Add API Service.getForegroundServiceType()
To return current foregroundServiceType if the service has become a
foreground service.
If service become foreground service by calling Service.startForeground(int,
Notification, int type), the returned type is the type specified.
If the no-type version Service.startForeground(int, Notification) is called.
the returned type is foregroundServiceType specified in manifest.
If no foregroundServiceType specified in manifest, the returned type is
zero.
If the service is not a foreground service, the returned type is zero.
Bug: 124517685
Test: atest cts/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java#testFgsLocation
Change-Id: Ibcc4431429a90aab92a5533e296fb104b4add9e6
Diffstat (limited to 'core/java/android/app/Service.java')
| -rw-r--r-- | core/java/android/app/Service.java | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/core/java/android/app/Service.java b/core/java/android/app/Service.java index f116e133e338..1f91b3f431a1 100644 --- a/core/java/android/app/Service.java +++ b/core/java/android/app/Service.java @@ -27,6 +27,7 @@ import android.content.ComponentName; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; +import android.content.pm.ServiceInfo; import android.content.pm.ServiceInfo.ForegroundServiceType; import android.content.res.Configuration; import android.os.Build; @@ -733,7 +734,7 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac * {@link android.R.attr#foregroundServiceType} flags. * @throws IllegalArgumentException if param foregroundServiceType is not subset of manifest * attribute {@link android.R.attr#foregroundServiceType}. - * @see {@link android.content.pm.ServiceInfo} for the set of FOREGROUND_SERVICE_TYPE flags. + * @see android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_MANIFEST */ public final void startForeground(int id, @NonNull Notification notification, @ForegroundServiceType int foregroundServiceType) { @@ -775,6 +776,30 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac } /** + * If the service has become a foreground service by calling + * {@link #startForeground(int, Notification)} + * or {@link #startForeground(int, Notification, int)}, {@link #getForegroundServiceType()} + * returns the current foreground service type. + * + * <p>If there is no foregroundServiceType specified + * in manifest, {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_NONE} is returned. </p> + * + * <p>If the service is not a foreground service, + * {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_NONE} is returned.</p> + * + * @return current foreground service type flags. + */ + public final @ForegroundServiceType int getForegroundServiceType() { + int ret = ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE; + try { + ret = mActivityManager.getForegroundServiceType( + new ComponentName(this, mClassName), mToken); + } catch (RemoteException ex) { + } + return ret; + } + + /** * Print the Service's state into the given stream. This gets invoked if * you run "adb shell dumpsys activity service <yourservicename>" * (note that for this command to work, the service must be running, and |
