What do I put into LocationManager call permission if statement
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I've been looking all over stackoverflow and search sites and have found so many different answers or outdated answers so I'm not sure how to answer this question. I am trying to get the latitude and longitude to use for my app. I've gotten to the point that I know to use location manager but then it calls for call permission.I've tried to find solutions on how to use it but there are so many answers with so many different views on whether those answers are correct. Here is my code, can some help me find the solution to what to put in the call permission for location manager and whether this code and get me the longitude and latitude? The results will be used in a fragment.
These are the permissions in my manifest file:ACCESS_FINE_LOCATION, INTERNET and
ACCESS_COARSE_LOCATION.
public class LocationFinder extends TestFragment implements LocationListener {
private static final String TAG = "LocationFragment";
private LocationManager mLocationManager;
private static double latitude;
private static double longitude;
public LocationFinder() {
mLocationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String permissions,
// int grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onStatusChanged(String s) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
public static double getLatitude(){
return latitude;
}
public static double getLongitude(){
return longitude;
}
}
java android locationmanager
add a comment |
I've been looking all over stackoverflow and search sites and have found so many different answers or outdated answers so I'm not sure how to answer this question. I am trying to get the latitude and longitude to use for my app. I've gotten to the point that I know to use location manager but then it calls for call permission.I've tried to find solutions on how to use it but there are so many answers with so many different views on whether those answers are correct. Here is my code, can some help me find the solution to what to put in the call permission for location manager and whether this code and get me the longitude and latitude? The results will be used in a fragment.
These are the permissions in my manifest file:ACCESS_FINE_LOCATION, INTERNET and
ACCESS_COARSE_LOCATION.
public class LocationFinder extends TestFragment implements LocationListener {
private static final String TAG = "LocationFragment";
private LocationManager mLocationManager;
private static double latitude;
private static double longitude;
public LocationFinder() {
mLocationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String permissions,
// int grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onStatusChanged(String s) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
public static double getLatitude(){
return latitude;
}
public static double getLongitude(){
return longitude;
}
}
java android locationmanager
add a comment |
I've been looking all over stackoverflow and search sites and have found so many different answers or outdated answers so I'm not sure how to answer this question. I am trying to get the latitude and longitude to use for my app. I've gotten to the point that I know to use location manager but then it calls for call permission.I've tried to find solutions on how to use it but there are so many answers with so many different views on whether those answers are correct. Here is my code, can some help me find the solution to what to put in the call permission for location manager and whether this code and get me the longitude and latitude? The results will be used in a fragment.
These are the permissions in my manifest file:ACCESS_FINE_LOCATION, INTERNET and
ACCESS_COARSE_LOCATION.
public class LocationFinder extends TestFragment implements LocationListener {
private static final String TAG = "LocationFragment";
private LocationManager mLocationManager;
private static double latitude;
private static double longitude;
public LocationFinder() {
mLocationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String permissions,
// int grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onStatusChanged(String s) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
public static double getLatitude(){
return latitude;
}
public static double getLongitude(){
return longitude;
}
}
java android locationmanager
I've been looking all over stackoverflow and search sites and have found so many different answers or outdated answers so I'm not sure how to answer this question. I am trying to get the latitude and longitude to use for my app. I've gotten to the point that I know to use location manager but then it calls for call permission.I've tried to find solutions on how to use it but there are so many answers with so many different views on whether those answers are correct. Here is my code, can some help me find the solution to what to put in the call permission for location manager and whether this code and get me the longitude and latitude? The results will be used in a fragment.
These are the permissions in my manifest file:ACCESS_FINE_LOCATION, INTERNET and
ACCESS_COARSE_LOCATION.
public class LocationFinder extends TestFragment implements LocationListener {
private static final String TAG = "LocationFragment";
private LocationManager mLocationManager;
private static double latitude;
private static double longitude;
public LocationFinder() {
mLocationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String permissions,
// int grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onStatusChanged(String s) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
public static double getLatitude(){
return latitude;
}
public static double getLongitude(){
return longitude;
}
}
java android locationmanager
java android locationmanager
edited Nov 16 '18 at 12:45
Hossam Hassan
3401420
3401420
asked Nov 16 '18 at 12:05
ksb ksb
4717
4717
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
To check the app's permissions for ACCESS_FINE_LOCATION
, you do run checkSelfPermission
as you have done:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { }
As your TODO states, you need to call requestPermissions:
ActivityCompat.requestPermissions(this, new String[{Manifest.permission.ACCESS_FINE_LOCATION}, 378);
where 378
is any integer you like (used next to check the result of the request):
Next the result of the request must be processed via an overriding method:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
if (requestCode == 378) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do whatever you need with permissions
} else {
// display a message or request again
}
}
}
I believe this is correct, my code is not crashing but is the user supposed to grant permission? If so that prompt is not showing up when I run my app. I'm also not sure what I'm supposed to put in the if statement of the onRequestPermissionResult, is it the location part of my code. I can provide the rest of my code if my question isn't clear.
– ksb
Nov 21 '18 at 3:05
Wherever you put the first if statement (containingcheckSelfPermission
) in the code is when it will ask the user for permissions - I typically solve this by creating a splash screen for the app and putting it in the class'sonCreate
method so it runs when the app first starts. In theonRequestPermissionsResult
if statement you might want to set an instance variable to the last latitude/longtitude which you can get by creating a Location Service provider. This link may be useful: developer.android.com/training/location/retrieve-current
– Tom
Nov 21 '18 at 14:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337572%2fwhat-do-i-put-into-locationmanager-call-permission-if-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To check the app's permissions for ACCESS_FINE_LOCATION
, you do run checkSelfPermission
as you have done:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { }
As your TODO states, you need to call requestPermissions:
ActivityCompat.requestPermissions(this, new String[{Manifest.permission.ACCESS_FINE_LOCATION}, 378);
where 378
is any integer you like (used next to check the result of the request):
Next the result of the request must be processed via an overriding method:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
if (requestCode == 378) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do whatever you need with permissions
} else {
// display a message or request again
}
}
}
I believe this is correct, my code is not crashing but is the user supposed to grant permission? If so that prompt is not showing up when I run my app. I'm also not sure what I'm supposed to put in the if statement of the onRequestPermissionResult, is it the location part of my code. I can provide the rest of my code if my question isn't clear.
– ksb
Nov 21 '18 at 3:05
Wherever you put the first if statement (containingcheckSelfPermission
) in the code is when it will ask the user for permissions - I typically solve this by creating a splash screen for the app and putting it in the class'sonCreate
method so it runs when the app first starts. In theonRequestPermissionsResult
if statement you might want to set an instance variable to the last latitude/longtitude which you can get by creating a Location Service provider. This link may be useful: developer.android.com/training/location/retrieve-current
– Tom
Nov 21 '18 at 14:34
add a comment |
To check the app's permissions for ACCESS_FINE_LOCATION
, you do run checkSelfPermission
as you have done:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { }
As your TODO states, you need to call requestPermissions:
ActivityCompat.requestPermissions(this, new String[{Manifest.permission.ACCESS_FINE_LOCATION}, 378);
where 378
is any integer you like (used next to check the result of the request):
Next the result of the request must be processed via an overriding method:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
if (requestCode == 378) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do whatever you need with permissions
} else {
// display a message or request again
}
}
}
I believe this is correct, my code is not crashing but is the user supposed to grant permission? If so that prompt is not showing up when I run my app. I'm also not sure what I'm supposed to put in the if statement of the onRequestPermissionResult, is it the location part of my code. I can provide the rest of my code if my question isn't clear.
– ksb
Nov 21 '18 at 3:05
Wherever you put the first if statement (containingcheckSelfPermission
) in the code is when it will ask the user for permissions - I typically solve this by creating a splash screen for the app and putting it in the class'sonCreate
method so it runs when the app first starts. In theonRequestPermissionsResult
if statement you might want to set an instance variable to the last latitude/longtitude which you can get by creating a Location Service provider. This link may be useful: developer.android.com/training/location/retrieve-current
– Tom
Nov 21 '18 at 14:34
add a comment |
To check the app's permissions for ACCESS_FINE_LOCATION
, you do run checkSelfPermission
as you have done:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { }
As your TODO states, you need to call requestPermissions:
ActivityCompat.requestPermissions(this, new String[{Manifest.permission.ACCESS_FINE_LOCATION}, 378);
where 378
is any integer you like (used next to check the result of the request):
Next the result of the request must be processed via an overriding method:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
if (requestCode == 378) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do whatever you need with permissions
} else {
// display a message or request again
}
}
}
To check the app's permissions for ACCESS_FINE_LOCATION
, you do run checkSelfPermission
as you have done:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { }
As your TODO states, you need to call requestPermissions:
ActivityCompat.requestPermissions(this, new String[{Manifest.permission.ACCESS_FINE_LOCATION}, 378);
where 378
is any integer you like (used next to check the result of the request):
Next the result of the request must be processed via an overriding method:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
if (requestCode == 378) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do whatever you need with permissions
} else {
// display a message or request again
}
}
}
edited Nov 16 '18 at 12:27
answered Nov 16 '18 at 12:21
TomTom
1006
1006
I believe this is correct, my code is not crashing but is the user supposed to grant permission? If so that prompt is not showing up when I run my app. I'm also not sure what I'm supposed to put in the if statement of the onRequestPermissionResult, is it the location part of my code. I can provide the rest of my code if my question isn't clear.
– ksb
Nov 21 '18 at 3:05
Wherever you put the first if statement (containingcheckSelfPermission
) in the code is when it will ask the user for permissions - I typically solve this by creating a splash screen for the app and putting it in the class'sonCreate
method so it runs when the app first starts. In theonRequestPermissionsResult
if statement you might want to set an instance variable to the last latitude/longtitude which you can get by creating a Location Service provider. This link may be useful: developer.android.com/training/location/retrieve-current
– Tom
Nov 21 '18 at 14:34
add a comment |
I believe this is correct, my code is not crashing but is the user supposed to grant permission? If so that prompt is not showing up when I run my app. I'm also not sure what I'm supposed to put in the if statement of the onRequestPermissionResult, is it the location part of my code. I can provide the rest of my code if my question isn't clear.
– ksb
Nov 21 '18 at 3:05
Wherever you put the first if statement (containingcheckSelfPermission
) in the code is when it will ask the user for permissions - I typically solve this by creating a splash screen for the app and putting it in the class'sonCreate
method so it runs when the app first starts. In theonRequestPermissionsResult
if statement you might want to set an instance variable to the last latitude/longtitude which you can get by creating a Location Service provider. This link may be useful: developer.android.com/training/location/retrieve-current
– Tom
Nov 21 '18 at 14:34
I believe this is correct, my code is not crashing but is the user supposed to grant permission? If so that prompt is not showing up when I run my app. I'm also not sure what I'm supposed to put in the if statement of the onRequestPermissionResult, is it the location part of my code. I can provide the rest of my code if my question isn't clear.
– ksb
Nov 21 '18 at 3:05
I believe this is correct, my code is not crashing but is the user supposed to grant permission? If so that prompt is not showing up when I run my app. I'm also not sure what I'm supposed to put in the if statement of the onRequestPermissionResult, is it the location part of my code. I can provide the rest of my code if my question isn't clear.
– ksb
Nov 21 '18 at 3:05
Wherever you put the first if statement (containing
checkSelfPermission
) in the code is when it will ask the user for permissions - I typically solve this by creating a splash screen for the app and putting it in the class's onCreate
method so it runs when the app first starts. In the onRequestPermissionsResult
if statement you might want to set an instance variable to the last latitude/longtitude which you can get by creating a Location Service provider. This link may be useful: developer.android.com/training/location/retrieve-current– Tom
Nov 21 '18 at 14:34
Wherever you put the first if statement (containing
checkSelfPermission
) in the code is when it will ask the user for permissions - I typically solve this by creating a splash screen for the app and putting it in the class's onCreate
method so it runs when the app first starts. In the onRequestPermissionsResult
if statement you might want to set an instance variable to the last latitude/longtitude which you can get by creating a Location Service provider. This link may be useful: developer.android.com/training/location/retrieve-current– Tom
Nov 21 '18 at 14:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337572%2fwhat-do-i-put-into-locationmanager-call-permission-if-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown