Как узнать текущее местоположение из GPRS на Android без GPS и подключения к интернету? - PullRequest
0 голосов
/ 18 января 2019

Мы можем получить текущее местоположение из GPS на фоновом сервисе. Но мы хотим получить текущее местоположение, когда пользователь выключил gps, и у него нет подключения к Интернету!

Мы обнаружили, что в телефонах XiaoMi приведенный ниже код работает нормально. Но в других телефонах, таких как Samsung, LG, мы не могли получить желаемый результат.

Так как же получить текущее местоположение без интернета и GPS в Samsung, LG и других телефонах, кроме Xiaomi?

package uz.codic.ahmadtea.ui.dashboard.maps;

    import android.Manifest;
    import android.app.Service;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.IBinder;
    import android.support.annotation.Nullable;
    import android.support.v4.app.ActivityCompat;
    import android.util.Log;

    import java.util.Timer;
    import java.util.TimerTask;

    public class LocationService extends Service implements LocationListener {
        boolean isGPSEnable = false;
        boolean isNetworkEnable = false;
        double latitude, longitude;
        LocationManager locationManager;
        Location location;
        private Handler mHandler = new Handler();
        private Timer mTimer = new Timer();
        long notify_interval = 1000;
        public static String str_receiver = "servicetutorial.service.receiver";
        Intent intent;
        private static final String TAG="MYLLOCC";

        public LocationService() {

        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();

            mTimer = new Timer();
            mTimer.schedule(new TimerTaskToGetLocation(), 5, notify_interval);
            intent = new Intent(str_receiver);
    //        fn_getlocation();
        }

        @Override
        public void onLocationChanged(Location location) {
            Log.d(TAG,"MMMMMMMMMM: "+location.getLatitude());
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d(TAG,"Status changed");
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.d(TAG,"Provide enabled");
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.d(TAG,"Provide dissabled");
        }

        private void fn_getlocation() {
            locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
            isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnable && !isNetworkEnable) {

            } else {

                if (isNetworkEnable) {
                    location = null;
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {

                            Log.e("latitude", location.getLatitude() + "");
                            Log.e("longitude", location.getLongitude() + "");

                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            fn_update(location);
                        }
                    }

                }


                if (isGPSEnable) {
                    location = null;
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                        return;
                    }
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
                    if (locationManager!=null){
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location!=null){
                            Log.e(TAG,"latitude"+location.getLatitude()+""); //Here taking location
                            Log.e(TAG,"longitude"+location.getLongitude()+""); //Here taking location
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            fn_update(location);
                        }
                    }
                }
            }

        }

        private class TimerTaskToGetLocation extends TimerTask {
            @Override
            public void run() {

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        fn_getlocation();
                    }
                });

            }
        }

        private void fn_update(Location location){

            intent.putExtra("latutide",location.getLatitude()+"");
            intent.putExtra("longitude",location.getLongitude()+"");
            sendBroadcast(intent);
        }

    }
...