-
Notifications
You must be signed in to change notification settings - Fork 5
/
locationnnnnn.txt
65 lines (55 loc) · 2.68 KB
/
locationnnnnn.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
long MIN_TIME = 5000;
float MIN_DISTANCE = 1000;
String LOCATION_PROVIDER = LocationManager.GPS_PROVIDER;
LocationManager mLocationManager;
LocationListener mLocationlistener;
final int REQUEST_CODE = 123;
@Override
protected void onResume() {
super.onResume();
getLocation();
}
private void getLocation() {
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationlistener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
String longitude = String.valueOf(location.getLongitude());
String lattitude = String.valueOf(location.getLatitude());
Log.d("loc",longitude);
Log.d("loc",lattitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
return;
}
mLocationManager.requestLocationUpdates(LOCATION_PROVIDER, MIN_TIME, MIN_DISTANCE, mLocationlistener);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == REQUEST_CODE){
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
getLocation();
}else{
Log.d("loc","Location Access Denied");
}
}
}