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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:03:33+00:00 2026-06-14T15:03:33+00:00

Hello stackoverflow’s world, this is my first post here. I’m a newer in Android

  • 0

Hello stackoverflow’s world,

this is my first post here.

I’m a newer in Android world so also if I checked online different tutorials I’ve problem with the enabling GPS settings that has to appear when I open my own Maps.

I’ve tryied the following code I found here: Android get location or prompt to enable location service if disabled

Button gpsButton = (Button)this.findViewById(R.id.buttonGPSLocation);
gpsButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // Start loction service
    LocationManager locationManager = (LocationManager)[OUTERCLASS].this.getSystemService(Context.LOCATION_SERVICE);

    Criteria locationCritera = new Criteria();
    locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
    locationCritera.setAltitudeRequired(false);
    locationCritera.setBearingRequired(false);
    locationCritera.setCostAllowed(true);
    locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);

    String providerName = locationManager.getBestProvider(locationCritera, true);

    if (providerName != null && locationManager.isProviderEnabled(providerName)) {
        // Provider is enabled
        locationManager.requestLocationUpdates(providerName, 20000, 100, [OUTERCLASS].this.locationListener);
    } else {
        // Provider not enabled, prompt user to enable it
        Toast.makeText([OUTERCLASS].this, R.string.please_turn_on_gps, Toast.LENGTH_LONG).show();
        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        [OUTERCLASS].this.startActivity(myIntent);
    }
}
});

But I receive a couple of errore, maybe because I cannot understand it as well !!!

These are my questions:

  • Why it’s necessary to declare a Button (gpsButton) and where/how I can create it?
  • The code can be overall putted after the class MapActivity or must be putted after the first onCreate?
  • What is OUTERCLASS?
  • A part of the permission on Manifest(I already have) I’ve to change something on Layout?

So, generally speaking someone can explain me better how I can use this code and what modifies I need to do.

If you need I can send out my entire java code.

Thanks in advance for your availability.

With regards,

Claudio

  • 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-14T15:03:34+00:00Added an answer on June 14, 2026 at 3:03 pm

    Well, you’ve got several questions here.

    Why it’s necessary to declare a Button (gpsButton) and where/how I can create it?

    If you want to create a button then you can declare it in either in the XML layout used by your Activity or else you can add it in code. If you’re simply pasting the code from some sample, you’ll need to understand that this code actually creates a reference to a Button object by finding it in the current view hiearchy (meaning the layout currently being displayed) by it’s id. So if you haven’t created a button with id of buttonGPSLocation somewhere in XML, then this code won’t work. As to why it’s necessary: it seems like the author of the sample decided to prompt the user to tap a button in order to open the phone setting and enable GPS.

    The code can be overall putted after the class MapActivity or must be putted after the first onCreate?

    This question isn’t very clear (might be a language issue here), but it might help if you post all the relevant code. In general, setting onClickListeners should probably be done in onCreate of your Activity. Whether it’s done inside of a MapActivity or not depends on more information that you haven’t supplied to us.

    What is OUTERCLASS?

    In Java, you can define a class inside of another class. We usually see classes defined as “top level” classes, i.e. some file names SomeClass.java, the contents of which look like

    public class SomeClass {
    //some code
    }
    

    Now, when you define a class inside of another class, the “nested” one is the inside of the other and not vice versa. So the one that contains the nested class is the “outer” class.

    class OuterClass {
        ...
        class NestedClass {
            ...
        }
    }
    

    Why does this all matter? Well because there are some rules about how things work between outer and nested classes. In your case what’s most relevant is the fact when you set an onClickListener on an button, what you’re actually doing is passing a reference to an instance of the OnClickListener class. In Java, you can create an anonymous inner class, which means that instead of defining some top level class that implements onClickListener (which btw, you can easily by having the class that all of this is happening in simply declare that it implements the onClickListener interface, e.g. public class WhateverMyClassNameIs extends Activity implements View.OnClickListener) you are actually just creating an unnamed, on the fly onClickListener instance which cannot be referenced anywhere else because you haven’t declared a reference to it. Now, the issue is that inside of this onClicklistener instance, when you want to now refer to the outer class that it’s defined in (in your case, the Activity class), you cannot just use “this” anymore since “this” is actually the onClickListener because of the way that “scope” works. In other words, an OnClickListener has no method called “startActivity”; only an Activity class does (or your class that extends Activity). So you have to use what’s called the “qualified this” to clarify that you intend to reference the “outer class”, which is why you need *MyActivity*.this.startActivity...

    A part of the permission on Manifest(I already have) I’ve to change
    something on Layout?

    The manifest and layout files are two totally different things. They are both defined using XML, but they’re totally different. The manifest, among other things, contains a declaration of all of your Activity classes and permissions that the app is requesting, etc. Layout files are the actual layouts used inside of Activities (the UI). They should be located in res/layout and you can define whatever you want there. When you need to then make that UI “alive” you have to grab references to your widgets (buttons, textviews) etc inside of your Activity in code and then do what you want there.

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

Sidebar

Related Questions

Hello stackoverflow guru's! This is my first post after having used the site for
Hello all! This is my first post on stackoverflow. After hours of searching and
Hello StackOverflow gurus. This is my first question on here so I am excited
Hello world of stackoverflow, nice to meet you all. My first question here is
Hello StackOverflow Community, I hope you guys can help me with this here: On
Hello StackOverflow Users, I am new to android and trying to develop a game
Suppose I have this code: String encoding = UTF-16; String text = [Hello StackOverflow];
Hello StackOverflow community, The air.swf file referenced here: http://livedocs.adobe.com/flex/3/html/help.html?content=distributing_apps_3.html used to launch AIR applications
Hello good people of stackoverflow, this is a conceptual question and could possibly belong
Hello all at StackOverflow.. Reasonably new to this site, but I have had nothing

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.