Issue
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
Solution
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.
Answered By - LuxuryMode
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.