THANK YOU! The word I was missing in my searches was Relection or
Reflect.

I've added this method to my Service, but I'm still having a
problem...

        private void turnOnForeground(Notification notif) {
                try {
                        Method m = Service.class.getMethod("startForeground", 
new Class[]
{int.class, Notification.class});
                        m.invoke(this, NOTIFY_ID, notif);
                } catch (Exception e) {
                        setForeground(true);
                        mNotificationManager.notify(NOTIFY_ID, notif);
                }

                //Debug message to show if turn on worked
                ActivityManager am = (ActivityManager) getBaseContext
().getSystemService(ACTIVITY_SERVICE);
                Toast.makeText(getBaseContext(), "Foreground: " +
am.getRunningServices(1).get(0).foreground, Toast.LENGTH_SHORT).show
();
        }

The problem is the Toast at the end is still reporting false (on 2.0,
works fine on <1.6), i.e. that the service isn't running in the
foreground state. The code runs through and I can see in the LogCat
that no exception is thrown or caught. Also, I tried just using
regular ol' startForeground, then showing that same Toast, and tested
on the 2.0 emulator, and it still reported false. Does that method of
retrieving the foreground status just not work in 2.0 anymore (it
works on 1.6)? How can I be sure my service is running in the
foreground on the 2.0 emulator?

On Nov 15, 4:21 pm, TreKing <[email protected]> wrote:
> Yes, this is possible and definitely testable in the emulator. I actually
> just update my own app to account for this change in 2.0.
>
> I don't think you can get around the VerifyError but you can check if the
> method you want to use exists. Build against 2.0 and use reflection to check
> if the startForeground exists and use it if it does, otherwise default to
> setForeground.
>
> There was a blog post about using reflection for things like this though I
> don't have the link handy.
>
> Hope that helps.
>
> --------------------------------------------------------------------------- 
> ----------------------
> TreKing - Chicago transit tracking app for Android-powered 
> deviceshttp://sites.google.com/site/rezmobileapps/treking
>
>
>
> On Sun, Nov 15, 2009 at 2:27 PM, G <[email protected]> wrote:
> > Hi, I've got a question as I don't have an Android 2.0 device to test
> > on and this particular aspect seems untestable in the emulator.
>
> > I've got a service that needs to be run as a foreground service. I
> > have a notification that stops it etc, so I have no problem using
> > startForeground instead of setForeground. However, I'd like to have 1
> > version of my app that works across all platforms... Is this possible?
> > If my app is compiled with Android 1.5 and uses setForeground, then
> > will that command actually work on an Android 2.0 device, or does it
> > not matter which sdk was used to compile? I've tried compiling with
> > 2.0, and surrounding the call to startForeground with a try/catch, but
> > that throws a VerifyError when the service is started, when run on a
> > 1.5/6 device since startForeground doesn't exist there. Can I catch
> > and ignore the VerifyError somehow and make it work that way?
>
> > Any help would be appreciated, I really don't want to have to make a
> > 2nd branch of my app.
>
> > Regards,
> > Geoff
>
> > --
> > 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%2Bunsubs 
> > [email protected]>
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en

-- 
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

Reply via email to