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 9237559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:33:41+00:00 2026-06-18T07:33:41+00:00

I have to submit my mini project. I write a class to get the

  • 0

I have to submit my mini project. I write a class to get the user location. I want to know if there is any technical problem in my approach or not. It works fine. I am Using a Handler to get the location continuously.

Main.java

public class Main extends Activity {

LocationTracker lt;
TextView tv;
Handler handler = new Handler();
Runnable locationRunner = new Runnable() {

    @Override
    public void run() {
        if (lt.canGetLocation()) {
            tv.setText(lt.getLatitude() + " " + lt.getLongitude());
        }
        handler.postDelayed(this, 1000);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lt = new LocationTracker(this);
    Toast.makeText(
            this,
            "GPS: " + lt.isGPSEnabled() + "\nNetwork: "
                    + lt.isNetworkEnabled() + "\nCanGetLocation: "
                    + lt.canGetLocation(), Toast.LENGTH_LONG).show();
    tv = (TextView) findViewById(R.id.textView1);
}

@Override
protected void onResume() {
    super.onResume();
    handler.postDelayed(locationRunner, 1000);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    handler.removeCallbacks(locationRunner);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

LocationTracker.java

public class LocationTracker implements LocationListener {

private Context context;
private boolean isGPSEnabled;
private boolean isNetworkEnabled;
private boolean isTracking;
private Location location;
private LocationManager locationManager;

public LocationTracker(Context context) {
    this.context = context;
    locationManager = (LocationManager) context
            .getSystemService(context.LOCATION_SERVICE);
    isNetworkEnabled = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    isGPSEnabled = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (!isNetworkEnabled && !isGPSEnabled) {
        isTracking = false;
    } else {
        isTracking = true;
    }

    if (isGPSEnabled) {
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, this);
    } else if (isNetworkEnabled) {
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, this);
    }

}

public double getLatitude() {
    if (location != null) {
        return location.getLatitude();
    } else {
        return 0.00;
    }
}

public double getLongitude() {
    if (location != null) {
        return location.getLongitude();
    } else
        return 0.00;
}

public boolean canGetLocation() {
    return isTracking;
}

public boolean isGPSEnabled() {
    return isGPSEnabled;
}

public boolean isNetworkEnabled() {
    return isNetworkEnabled;
}

@Override
public void onLocationChanged(Location arg0) {
    location = arg0;
}

@Override
public void onProviderDisabled(String arg0) {
}

@Override
public void onProviderEnabled(String arg0) {
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

}

}

`

  • 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-06-18T07:33:43+00:00Added an answer on June 18, 2026 at 7:33 am

    Yeah, As I see it, 2 things are wrong.

    1. Don’t check for availability to requestLocationUpdates.
    2. Instead listen to DATA connection changes and removeUpdates, and requestLocationUpdates.

    Sample code

    //OnCreate

    tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(new NetworkConnectionState(this.getApplicationContext()),
                PhoneStateListener.LISTEN_DATA_CONNECTION_STATE 
    | PhoneStateListener.LISTEN_CELL_LOCATION );
    

    //Your listener

    public void onDataConnectionStateChanged(int state, int networkType) {
            // We have changed protocols, for example we have gone from HSDPA to
            // GPRS
            // HSDPA is an example of a 3G connection
            // GPRS is an example of a 2G connection
            if (state == TelephonyManager.DATA_CONNECTED) {
            }
            else {
            }
        }
    

    Edit :
    Dont forget to update your connection / disconnection on onProviderDisabled and onProviderEnabled in your LocationListeners

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Submit button to remove a user: <form class=spform action=<?=$_SERVER['PHP_SELF']?> method=post name=userdetails
I have two submit buttons. I want user to click first submit button then
I have a submit button that changes when the user hovers his mouse over
I have a submit button where I want a confirmation pop-up, doing this works
I have submit my app to apple store and now I want to send
I have a submit button and I want to show an image(a.jpg) on that
I have a submit button with a markup like this: <li class=button><div class=button> <input
I have a submit form for a URL and I want it to have
I have a mini form on the home page which allows the user to
I have 2 submit buttons and want to perform different actions for each submit

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.