Latitude and Longitude showing as 0 in Android

后端 未结 2 727
一向
一向 2021-01-14 18:39
  ************* TrackGPS.java *****************************  

    import android.app.AlertDialog;
    import android.app.Service;
    import android.content.Context         


        
相关标签:
2条回答
  • 2021-01-14 19:22

    Here is the background service to detect current location at time interval

    import android.annotation.SuppressLint;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.location.Location;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.support.annotation.Nullable;
    import android.util.Log;
    import android.widget.Toast;
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GooglePlayServicesUtil;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.common.api.PendingResult;
    import com.google.android.gms.common.api.Status;
    import com.google.android.gms.location.LocationListener;
    import com.google.android.gms.location.LocationRequest;
    import com.google.android.gms.location.LocationServices;
    
    @SuppressWarnings("MissingPermission")
    public class LocationBackGroundService extends Service implements LocationListener,
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener {
    
        private static final String TAG = "LocationBackGroundService";
        private static final long INTERVAL = 10;
        private static final long FASTEST_INTERVAL = 10;
    
        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()) {
    
            }
            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();
        }
    
        @SuppressLint("LongLogTag")
        protected void startLocationUpdates() {
            PendingResult<Status> 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());
    
                ConstantFunction.saveStatus(this, ConstantVariables.CURRENT_LATITUDE, lat);
                ConstantFunction.saveStatus(this, ConstantVariables.CURRENT_LONGTITUDE, lng);
                System.out.println("First Condition Hit");
                System.out.println("Lat::-- " + lat + "\n" + "LONG::--" + lng);
            }
    
            System.out.println("Lat::-- " + location.getLatitude() + "\n" + "LONG::--" + location.getLongitude());
        }
    }
    

    Put this code in seperate java file named LocationBackGroundService.java and call it in your MainActivity like this startService(new Intent(this, LocationBackGroundService.class)); and don't forget to add it in menifest like this <service android:name="LocationBackGroundService"></service>

    0 讨论(0)
  • 2021-01-14 19:42

    Please manage required permission for marshmallow.

    First you add this permission in manifest file

     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

    After in your activity First declare two variable like this,

     private static final int REQUEST_CODE_PERMISSION = 1;
     String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;
    

    After in onCreate method

     if(Build.VERSION.SDK_INT>= 23) {
    
            if (checkSelfPermission(mPermission) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{mPermission,
                        },
                        REQUEST_CODE_PERMISSION);
                return;
            }
    
            else
            {
                *here manage your code if permission already access
            }
        }
    
    0 讨论(0)
提交回复
热议问题