Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6561137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:33:01+00:00 2026-05-25T13:33:01+00:00

I have application using Android 2.1 which utilize LocationManager to get the altitude. But

  • 0

I have application using Android 2.1 which utilize LocationManager to get the altitude. But now, I need to obtain the altitude using SensorManager which requires API Level 9 (2.3).

How can I put the SensorManager.getAltitude(float, float) in my 2.1 android application by putting a condition and calling it by a function name (possible in normal Java)?

Thank you in advance

UPDATE 1
If you have noticed that my application need to be compiled using Android 2.1. That’s why I’m looking for a way to call the function by name or in any other way that can be compiled.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:33:02+00:00Added an answer on May 25, 2026 at 1:33 pm

    You can call the method using reflection and fail gracefully in case of errors (like missing class or methods). See java.lang.reflect

    Other option is to compile code in level 9 but surround with try/catch to catch errors that would arise from execution on lower level. It could be fairly error prone, though, and I’d think twice about doing it.


    Update

    Here is test code

    public void onCreate(Bundle savedInstanceState)
    {
        try {
            // First we try reflection approach.
            // Expected result
            //    in 2.3 we print some value in log but no exception
            //    in 2.2 we print NoSuchMethodException
            // In both levels we get our screen displayed after catch
            Method m = SensorManager.class.getMethod("getAltitude",Float.TYPE, Float.TYPE);
            Float a = (Float)m.invoke(null, 0.0f, 0.0f);
            Log.w("test","Result 1: " + a);
        } catch (Throwable e) {
            Log.e("test", "error 1",e);
        }
    
        try {
            // Now we try compiling against 2.3
            // Expected result
            //    in 2.3 we print some value in log but no exception
            //    in 2.2 we print NoSuchMethodError (Note that it is an error not exception but it's still caught)
            // In both levels we get our screen displayed after catch
            float b = SensorManager.getAltitude(0.0f, 0.0f);
            Log.w("test","Result 2: " + b);
    
        } catch (Throwable e) {
            Log.e("test", "error 2",e);
        }
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    

    Results:

    2.3

    09-14 07:04:50.374: DEBUG/dalvikvm(589): Debugger has detached; object registry had 1 entries
    09-14 07:04:50.924: WARN/test(597): Result 1: NaN
    09-14 07:04:51.014: WARN/test(597): Result 2: NaN
    09-14 07:04:51.384: INFO/ActivityManager(75): Displayed com.example/.MyActivity: +1s65ms
    

    2.2

    09-14 07:05:48.220: INFO/dalvikvm(382): Could not find method android.hardware.SensorManager.getAltitude, referenced from method com.example.MyActivity.onCreate
    09-14 07:05:48.220: WARN/dalvikvm(382): VFY: unable to resolve static method 2: Landroid/hardware/SensorManager;.getAltitude (FF)F
    09-14 07:05:48.220: DEBUG/dalvikvm(382): VFY: replacing opcode 0x71 at 0x0049
    09-14 07:05:48.220: DEBUG/dalvikvm(382): VFY: dead code 0x004c-0064 in Lcom/example/MyActivity;.onCreate (Landroid/os/Bundle;)V
    09-14 07:05:48.300: ERROR/test(382): error 1
            java.lang.NoSuchMethodException: getAltitude
            at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
    

    Skipped stack trace

    09-14 07:05:48.300: ERROR/test(382): error 2
        java.lang.NoSuchMethodError: android.hardware.SensorManager.getAltitude
        at com.example.MyActivity.onCreate(MyActivity.java:35)
    

    Skipped more stack trace

    09-14 07:05:48.330: DEBUG/dalvikvm(33): GC_EXPLICIT freed 2 objects / 64 bytes in 180ms
    09-14 07:05:48.520: INFO/ActivityManager(59): Displayed activity com.example/.MyActivity: 740 ms (total 740 ms)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Android application which is Sales Rep application.When he using the app first
I need to build Android application using Java which is able to send request
I have an Android application which needs username and password to login. I need
I have an android application which allows the user to take a picture using
I have an Android application which retrieves information from the web using an AsyncTask
I have written an application in C which I am porting to Android using
I need to build my Android application using the command line, when I get
I have an Android Application which uses JNI calls. Now i want this application
I have a Php application using stream_socket_client(), to get data through tcp from a
I have a control application - using asp.net webservices. I have a timer which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.