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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:47:18+00:00 2026-06-17T12:47:18+00:00

My Gps settings are off in the beginning and I executed this code,and I

  • 0

My Gps settings are “off” in the beginning and I executed this code,and I can able to get CurrentLocation latitude and longitude.

But once I turned ON programatically,the settings are changed and everything is fine.

When I executed again,it is not giving me (provider,location references),I didnt understand why it is happening.

 public void getCurrentlocation()
{
    String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    Log.e("provider",providers);
    if(!providers.contains("gps")){
      final Intent poke = new Intent();
      poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
      poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
      poke.setData(Uri.parse("3")); 
      sendBroadcast(poke);
      Log.e("your gps is","enabled");
    }
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       // Define the criteria how to select the locatioin provider -> use
       // default
       Criteria criteria = new Criteria();
       String provider = locationManager.getBestProvider(criteria,true);
       Location location = locationManager.getLastKnownLocation(provider);

       // Initialize the location fields
       if (location != null) 
       {
        System.out.println("Provider " + provider + " has been selected.");
              strLatitude=String.valueOf(location.getLatitude());
              strLongitude=String.valueOf(location.getLongitude());
              Log.e("lattitude",strLatitude);
              Log.e("logntitude",strLongitude);
       }
       else 
       {
              strLatitude=String.valueOf(0);
              strLongitude=String.valueOf(0);
              Log.e("lattitude",strLatitude);
              Log.e("logntitude",strLongitude);
       }

       Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

       try {
  List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(strLatitude), Double.parseDouble(strLongitude), 5);
 getAddress(Double.parseDouble(strLatitude), Double.parseDouble(strLongitude));
  if(addresses != null) {
   Address returnedAddress = addresses.get(0);
   StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
   for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
    strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
   }
   Log.e("address",strReturnedAddress.toString());
  }
  else{
   Log.e("not found","No Address returned!");
  }
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  Log.e("cannot get address","Canont get Address!");
 }
    }
  • 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-17T12:47:19+00:00Added an answer on June 17, 2026 at 12:47 pm

    Try this.

    Location getGPSLocation(Context context) {
        try {
            LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            Criteria cr = new Criteria();
            cr.setCostAllowed(true);
            cr.setAccuracy(Criteria.NO_REQUIREMENT);
            cr.setPowerRequirement(Criteria.NO_REQUIREMENT);
            String provider = lm.getBestProvider(cr, true);
            if(provider == null)
                provider = lm.getBestProvider(cr, false);
            return lm.getLastKnownLocation(provider);
        }
        catch(Exception e) {
            Log.e("NeartoMe", "Exception while fetch GPS at: " + e.getMessage());
        }
        return null;
    }
    

    Called the above method like this. If here you are getting null means problem in fetching the gps then redirect to enable the “Use Wireless Network” in location & security settings. Then try again to fetch the GPS.

    Location location = new Helper().getGPSLocation(context);
    if(location != null) {
        latitude = (int) (location.getLatitude() * 1E6);
        longitude = (int) (location.getLongitude() * 1E6);
    } else {
        Builder alert = new Builder(context);
        alert.setTitle(R.string.app_name);
        alert.setMessage(context.getString(R.string.enable_use_wireless_network));
        alert.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
                context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            }
        });
        alert.show();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to this, but I can't see any problem. There's a checkbox in settings
I try to send gps longitude and latitude to the android emulator with eclipse,
Given this code if (!locator.isProviderEnabled(LocationManager.GPS_PROVIDER)) { //consider forcing user to turn on gps here
I need to be able to toggle the GPS receiver on and off, and
I'm writing an app that needs to get the longitude and latitude from the
I am using the following code to get my current location. But the problem
Can we get the a rough location of iPhone without using GPS. Something region
Below is my code to alert the user whenever GPS is off. Here my
Can any trig or GPS experts help me out here? I'm trying to create
I have started my GPS programmatically by Intent myIntent = new intent(Settings.ACTION_SECURITY_SETTINGS); mContext.startActivity(myIntent); How

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.