Running a service to get location details

前端 未结 3 1809
日久生厌
日久生厌 2021-02-11 02:25

In my app, i need to create an android service to periodically get mobile user\'s current location(latitude and longitude). The service should get user\'s location in every 5 mi

3条回答
  •  一整个雨季
    2021-02-11 03:10

     LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);   
          LocationListener mlocListener = new MyLocationListener();
          mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
    }
     public class MyLocationListener implements LocationListener
        {
    
    
            @Override
            public void onLocationChanged(Location loc)
            {
                Log.i("**************************************", "Location changed");
    
                loc.getLatitude();
                loc.getLongitude(); 
                String Text = "My current location is: " +"Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();             
                Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onProviderDisabled(String provider)
            {
                Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
            }
    
            @Override
            public void onProviderEnabled(String provider)
            {
                Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras)
            {
            }
    
        }
    

提交回复
热议问题