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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:19:15+00:00 2026-05-21T11:19:15+00:00

I am not able to get the location even though the GPS is enabled

  • 0

I am not able to get the location even though the GPS is enabled and all location managers and listeners are set correctly. Here’s what I have so far:

public class GpsTest extends Activity  {
     private TextView text; 
     private LocationManager manager; 
     private LocationListener listener;
     private  Location myLocation;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text = (TextView)findViewById(R.id.textView1);
 try
 {
    manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    myLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //"gps"
    text.setText(("Lat: " + myLocation.getLatitude() 
                + "\nLLong: " + myLocation.getLongitude())); 
    listener = new MyLocationListener(); 
    manager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER, 
                                    0, 
                                    1, 
                                    listener);
    if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){  
        Toast.makeText(this, "GPS is enable. ", Toast.LENGTH_SHORT).show();  
  }  
    else createGpsDisabledAlert();   
 }catch(Exception ex)
 {
     createGpsDisabledAlert(); 
 }
        } 


public void onclicklistener(View v){
    try
    {
      text.setText(("Lat: " + myLocation.getLatitude() 
              + "\nLLong: " + myLocation.getLongitude()));
    }catch(Exception ex)
    {
     Toast.makeText(this, "Error while getting locations. "+ex.toString(), Toast.LENGTH_SHORT).show();   
    }
  }
  private void createGpsDisabledAlert(){  
      AlertDialog.Builder builder = new AlertDialog.Builder(this);  
      builder.setMessage("GPS is disabled! Would you like to enable it?")  
           .setCancelable(false)  
           .setPositiveButton("Enable GPS",  
                new DialogInterface.OnClickListener(){  
                public void onClick(DialogInterface dialog, int id){  
                     showGpsOptions();  
                }  
           });  
           builder.setNegativeButton("No Thanks",  
                new DialogInterface.OnClickListener(){  
                public void onClick(DialogInterface dialog, int id){  
                     dialog.cancel();  
                }  
           });  
      AlertDialog alert = builder.create();  
      alert.show();  
      }   
  private void showGpsOptions(){  
      Intent gpsOptionsIntent = new Intent(  
              android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
      startActivity(gpsOptionsIntent);  
  }  



   private class MyLocationListener implements LocationListener{ 
                public void onLocationChanged(Location location) { 

                        // TODO Auto-generated method stub 
                        if (location != null){ 
                                text.setText(("Lat: " + location.getLatitude() 
                                        + "\nLLong: " + location.getLongitude())); 
                        } 
                        Toast.makeText(GpsTest.this, ""+location, Toast.LENGTH_SHORT).show();
                } 
                public void onProviderDisabled(String provider) { 
                        // TODO Auto-generated method stub 
                } 
                public void onProviderEnabled(String provider) { 
                        // TODO Auto-generated method stub

                }
                @Override
                public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                    // TODO Auto-generated method stub

                } 
                } 
   public void onStatusChanged(String provider, int status, 
                Bundle extras) { 
                        // TODO Auto-generated method stub
                } 
 }
  • 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-21T11:19:16+00:00Added an answer on May 21, 2026 at 11:19 am

    You must to use a thread with his run() method like this (Is in spanish, i hope this not a problem for you)

    @Override
    public void run() {
    
        mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
    
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Looper.prepare();
            mToast.Make(getContext(),"GPS",0);
            mLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
            Looper.loop(); 
            Looper.myLooper().quit(); 
    
        } else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
            Looper.prepare();
            mToast.Make(getContext(),"Triangulacion",0);
            mLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
            Looper.loop();
            Looper.myLooper().quit();
        }else{
            mToast.Make(context,"No se encuentra señal se procede a mandar un mensaje normal",0);
            Looper.prepare();
            handlerNormal.sendEmptyMessage(0);
            Looper.loop();
            Looper.myLooper().quit();
        }   
    
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have few Question for which I am not able to get a proper
I am using CLLocationManager to grab the location.Though I have GPS settings working on
I am not able to get my jwysiwyg and Jhtmlarea text editors to work
I've read the DateFormatting guide and I'm still not able to get a working
I know this is simple but I am not able to get it right
Trying out the examples found on pleac.sf.net , I'm not able to get an
This is an open ended interview question. I am not able to get a
I am learning Java Security JCE/JAAS. I am not able to get a example
I am able to get the output format that I need, but not the
I am not able to flush stdin here, is there a way to flush

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.