How to update longitude and latitude in every 10 seconds in mysql database?

前端 未结 3 603
鱼传尺愫
鱼传尺愫 2020-12-22 00:52

I\'m trying to get latitude and longitude from Android device and send it to MySQL database. when any one register his current latitude and longitude saved in the database.b

3条回答
  •  盖世英雄少女心
    2020-12-22 01:26

    Create a background service class like this

    public class LocationBackGroundService extends Service implements LocationListener,
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener {
    
        private static final String TAG = "LocationBackGroundService";
        private static final long INTERVAL = 100;
        private static final long FASTEST_INTERVAL = 100;
    
        LocationRequest mLocationRequest;
        GoogleApiClient mGoogleApiClient;
        Location mCurrentLocation;
        Context mCOntext;
    
        public void LocationBackGroundService(Context mContext) {
            this.mCOntext = mContext;
        }
    
        protected void createLocationRequest() {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(INTERVAL);
            mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        }
    
        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            mGoogleApiClient.connect();
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            if (!isGooglePlayServicesAvailable()) {
                // finish();
            }
            createLocationRequest();
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
    
            if (mGoogleApiClient.isConnected()) {
                startLocationUpdates();
            }
        }
    
        private boolean isGooglePlayServicesAvailable() {
            int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if (ConnectionResult.SUCCESS == status) {
                return true;
            } else {
                return false;
            }
        }
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onConnected(Bundle bundle) {
            startLocationUpdates();
        }
    
    
        protected void startLocationUpdates() {
            PendingResult pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, this);
    
            Log.d(TAG, "Location update started ..............: ");
        }
    
        @Override
        public void onConnectionSuspended(int i) {
    
            Toast.makeText(this, "OnConnection Suspended", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Toast.makeText(this, "OnConnection Failed", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onLocationChanged(Location location) {
    
            if (null != mCurrentLocation) {
                mCurrentLocation = location;
                String lat = String.valueOf(mCurrentLocation.getLatitude());
                String lng = String.valueOf(mCurrentLocation.getLongitude());
    
            }
        }
    }
    

    And call when activity starts like this

    startService(new Intent(this, yourBackgroundServiceName.class));
    

    and in menifest

    
    

    and don;t forget to add run time permissions before stating a service

提交回复
热议问题