Hello there.
My point is to be able to call a 2.1 API if it exists, while the
application should be able to run in 1.5
Specifically, I am making a GPS application which would like to call
android.location.LocationManager.addNmeaListener if available.
My problem is that this call takes as parameter an abstract class
(GpsStatus.NmeaListener) which I cannot pass directly to
Class.GetMethod (or my application won't work in 1.5).
What I am currently trying is a) to create a wrapper class for
GpsStatus.NmeaListener:
public class APILevel5
{
// class initialization fails when this throws an exceptio
static
{
try {
//
Class.forName("android.location.GpsStatus.NmeaListener");
// We don't have to check for existance because it
is an abstract
class
}
catch (Exception ex)
{
throw new RuntimeException(ex);
}
}
// calling here forces class initialization
public static void checkAvailable() {}
// Forced Constructor
public static GpsStatus.NmeaListener Creator()
{
GpsStatus.NmeaListener mGpsNmeaListener = new
GpsStatus.NmeaListener()
{
@Override
public void onNmeaReceived(long timestamp, String nmea)
{
}
};
return mGpsNmeaListener;
}
}
When checkAvailable() is called from 1.5, it throws. When called from
2.1, it doesn't. All good so far.
Now how to call the method ? I am using this reflection code:
public static Object CallMethod(Class x,String mn,Class... typs)
{
try
{
m = x.getMethod(mn, typs);
return m.invoke(null,(Object)typs);
}
catch(NoSuchMethodException e)
{
}
catch(InvocationTargetException e)
{
}
catch (IllegalAccessException ie)
{
}
return null;
}
When I call CallMethod(LocationManager.class,"addNmeaListener" , new
Class[] { GpsStatus.NmeaListener.class }); , it works in 2.1 but the
application doesn't run at all in 1.5
When I call CallMethod(LocationManager.class,"addNmeaListener" , new
Class[] { APILevel5.class }); , the application runs in 1.5 but
doesn't find the method because APILevel5.class is passed and not
GpsStatus.NmeaListener.class.
How can I resolve all this Java stuff ?
Best Regards,
Michael.
--
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