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

  • Home
  • SEARCH
  • 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 6389939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:27:59+00:00 2026-05-25T03:27:59+00:00

Java/Android noob here (Still) I hoped you could help. I was trying to add

  • 0

Java/Android noob here (Still) I hoped you could help.

I was trying to add a small convenience method to get the number of time that has passed rather than the number of time since EPOCH.

So I tried to do this:

public class EasyLocation extends android.location.Location {

    ...

    public long getElapsedTime() {
        return getTime() - System.currentTimeMillis();
    }
}

However when I tried to use it like in the code below which casts it to my Location class. I got a class cast exception. I’ve read up on it and I fully understand why it doesn’t work. That’s not the issue.

The issue is: it’s not a train smash but I would like to have what is returned to have my method on it to make the rest of my code a bit cleaner. I can’t figure out a way around it. Any ideas on how to do that?

lastNetworkLocation = (EasyLocation) locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

Thanks,

Gerard.

  • 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-25T03:27:59+00:00Added an answer on May 25, 2026 at 3:27 am

    You can’t do it like that. (A Location is not an EasyLocation.)

    I believe what you’re looking for is the decorator pattern.

    You would typically do something like this:

    public class EasyLocation extends android.location.Location {
    
        public EasyLocation(Location loc) {
            // copy data from loc to this.
            super(loc);
        }
        ...
    
        public long getElapsedTime() {
            return getTime() - System.currentTimeMillis();
        }
    }
    

    And instead of doing

    lastNetworkLocation =
        (EasyLocation) locationManager.getLastKnownLocation(
                                                  LocationManager.NETWORK_PROVIDER);
    

    you do

    lastNetworkLocation =
        new EasyLocation(locationManager.getLastKnownLocation(
                                                  LocationManager.NETWORK_PROVIDER));
    

    Keep in mind though that most people would probably say “Favor composition over inheritance”, and go with

    public class EasyLocation {
    
        Location loc;
    
        public EasyLocation(Location loc) {
            this.loc = loc;
        }
    
        public long getElapsedTime() {
            return getTime() - System.currentTimeMillis();
        }
    
        public Location getLocation() {
            return loc;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Android application uses Java OAuth library, found here for authorization on Twitter. I
It seems the java.net.NetworkInterface implementation of android does not have a byte[] getHardwareAddress() method
Just started writing java / android and I am trying to use android.widget.VideoView. I
Okay, first, I'm a noob at android programming. I have taken some Java, but
I am coding in java for android. the issue is how to get mp3
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I'm trying
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I'm trying
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I'm trying
I am new to Java / Android development, and I am now trying to
I'm trying to learn to use xml in Java (Android platform, using Eclipse and

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.