Here is code that will allow you to run correctly on older and newer
platforms. This is part of a blog post I am working on that should be up in
the next few weeks.
private static final Class[] mStartForegroundSignature = new Class[] {
int.class, Notification.class};
private static final Class[] mStopForegroundSignature = new Class[] {
boolean.class};
private NotificationManager mNM;
private Method mStartForeground;
private Method mStopForeground;
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1];
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
try {
mStartForeground = getClass().getMethod("startForeground",
mStartForegroundSignature);
mStopForeground = getClass().getMethod("stopForeground",
mStopForegroundSignature);
} catch (NoSuchMethodException e) {
// Running on an older platform.
mStartForeground = mStopForeground = null;
}
}
/**
* This is a wrapper around the new startForeground method, using the
older
* APIs if it is not available.
*/
void startForegroundCompat(int id, Notification notification) {
// If we have the new startForeground API, then use it.
if (mStartForeground != null) {
mStartForegroundArgs[0] = Integer.valueOf(id);
mStartForegroundArgs[1] = notification;
try {
mStartForeground.invoke(this, mStartForegroundArgs);
} catch (InvocationTargetException e) {
// Should not happen.
Log.w("MyApp", "Unable to invoke startForeground", e);
} catch (IllegalAccessException e) {
// Should not happen.
Log.w("MyApp", "Unable to invoke startForeground", e);
}
return;
}
// Fall back on the old API.
setForeground(true);
mNM.notify(id, notification);
}
/**
* This is a wrapper around the new stopForeground method, using the
older
* APIs if it is not available.
*/
void stopForegroundCompat(int id) {
// If we have the new stopForeground API, then use it.
if (mStopForeground != null) {
mStopForegroundArgs[0] = Boolean.TRUE;
try {
mStopForeground.invoke(this, mStopForegroundArgs);
} catch (InvocationTargetException e) {
// Should not happen.
Log.w("MyApp", "Unable to invoke stopForeground", e);
} catch (IllegalAccessException e) {
// Should not happen.
Log.w("MyApp", "Unable to invoke stopForeground", e);
}
return;
}
// Fall back on the old API. Note to cancel BEFORE changing the
// foreground state, since we could be killed at that point.
mNM.cancel(id);
setForeground(false);
}
On Thu, Nov 26, 2009 at 1:53 PM, skyhigh <[email protected]> wrote:
> I have a couple of services in my application which need to be marked
> as foreground to prevent them from being killed unnecessarily.
>
> Currently I am building my application with the 2.0 SDK but I have
> android:minSdkVersion="3"
> android:targetSdkVersion="5"
> in my manifest file so that I can support 1.5, 1.6 or 2.0 devices. I
> am currently using only the 1.5 APIs so that my application will run
> correctly on the 1.5 devices.
>
> 2.0 introduced a nicer startForground method that combines setting the
> foreground flag and starting a notification which can be used to
> control the service. The 2.0 documentation says that startForground
> replaces setForeground, and that due to the fact that many
> applications were mistakenly using setForeground without a
> notification that could be used to control and close down the service,
> that the older setForeground method had been changed so it no longer
> does anything on 2.0.
>
> I tried using the newer 2.0 interface but it caused an exception for
> phones running 1.5 or 1.6. So I am still using the older
> setForeground interface and calling notify separately. But it appears
> that since I am building with the 2.0 SDK the older interface isn't
> really preventing my services from being killed.
>
> I would appreciate some guidance on the proper way to use these APIs
> so that my appliction will work correctly on 1.5, 1.6 and 2.0 devices.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>
--
Dianne Hackborn
Android framework engineer
[email protected]
Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails. All such
questions should be posted on public forums, where I and others can see and
answer them.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en