Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  3.  
  4. public class ActivityLogin extends AppCompatActivity {
  5. public static LocationManager mLocationManager;
  6. public static MyLocationListener locationListener = new MyLocationListener();
  7. ...
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. ...
  11. ActivityCompat.requestPermissions(ActivityLogin.this,
  12. new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  13. 2);
  14. ...
  15. @Override
  16. public void onRequestPermissionsResult(int requestCode,
  17. String permissions[], int[] grantResults) {
  18. switch (requestCode) {
  19. case 2: {
  20. // If request is cancelled, the result arrays are empty.
  21. if (grantResults.length > 0
  22. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  23. mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000, 0, locationListener);
  24. mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10*1000, 0, locationListener);
  25. mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 10*1000, 0, locationListener);
  26. }
  27. return;
  28. }
  29.  
  30. package com.example.god.italy36;
  31.  
  32. import android.location.Location;
  33. import android.location.LocationListener;
  34. import android.location.LocationManager;
  35. import android.os.Bundle;
  36.  
  37. import com.google.android.gms.maps.model.LatLng;
  38.  
  39. public class MyLocationListener implements LocationListener {
  40.  
  41.  
  42.  
  43. public void onLocationChanged(Location location) {
  44. //вызывается когда локация изменилась
  45. MapsActivity.Mylocation = new LatLng(location.getLatitude(), location.getLongitude());
  46. }
  47.  
  48. public void onProviderDisabled(String provider) {
  49. //вызывается когда провайдер отключается от пользователя
  50.  
  51. }
  52.  
  53. public void onProviderEnabled(String provider) {
  54. //вызывается когда провайдер включается
  55. Location location = ActivityLogin.mLocationManager.getLastKnownLocation(provider);
  56. /*if(ActivityLogin.mLocationManager.isProviderEnabled( LocationManager.GPS_PROVIDER))
  57. {
  58. location = ActivityLogin.mLocationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
  59. MapsActivity.Mylocation = new LatLng(location.getLatitude(), location.getLongitude());
  60. }*/
  61. if(location!= null)
  62. MapsActivity.Mylocation = new LatLng(location.getLatitude(), location.getLongitude());
  63. }
  64.  
  65. public void onStatusChanged(String provider, int status, Bundle extras) {
  66. //вызывается при изменении статуса провайдера
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement