如何在没有GPS和互联网连接的情况下从Android上的GPRS获取当前位置?

时间:2019-01-18 13:42:37

标签: android gps location gprs

我们可以在后台服务中从GPS获取当前位置。但是,当用户关闭gps并且没有Internet连接时,我们希望获得当前位置!

我们发现在小米手机中,以下代码可以正常工作。但是在LG,三星等其他手机中,我们无法获得理想的结果。

那么我们如何在没有小米的三星,LG其他手机中没有互联网和GPS的情况下获得当前位置?

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);
        }

    }

0 个答案:

没有答案