重启设备后,位置更新将停止使用NETWORK_PROVIDER

时间:2014-01-20 11:33:18

标签: android gps android-networking

在我的应用程序中,位置更新在重启设备后停止。 我使用gps和网络在我的应用程序中查找当前位置,重新启动gps工作正常后但是当我关闭gps时,应用程序很遗憾地关闭。 这是我的问题

这是我的代码Servicestart.java

    public class Servicestart extends Service {


     boolean gps_enabled = false;
     boolean network_enabled = false;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    Log.i("service started", "start");


      final LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      if(userFunctions.isUserLoggedIn(getApplicationContext())){




    if(Broadcast.check==false)
    {   
        LocationListener locatioListner = new LocationListener() {


            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
                get();
            }

            public void onLocationChanged(Location location) {

                Log.i("location", "loc");
                String latitude=String.valueOf(location.getLatitude());
                String longtitude=String.valueOf(location.getLongitude());
                //location updated starts here



                }

        };
        locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locatioListner);

    }





      }

}

void get()
{

     final LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
     gps_enabled = locMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
     network_enabled = locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

     if (!gps_enabled && !network_enabled) {  Context context = getApplicationContext();
     int duration = Toast.LENGTH_SHORT;
     Toast toast = Toast.makeText(context, "nothing is enabled", duration);
     toast.show();

 }



     LocationListener locatioListnerGps = new LocationListener() {

            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            public void onProviderEnabled(String provider) {

                Toast.makeText(getApplicationContext(), "gpsenabled", 1000).show();
            }

            public void onProviderDisabled(String provider) {
                Toast.makeText(getApplicationContext(), "gps disable", 1000).show();

                get();

            }

            public void onLocationChanged(Location location) {

                String latitude=String.valueOf(location.getLatitude());
                String longtitude=String.valueOf(location.getLongitude());
                //location updated starts here

            }
        };
         LocationListener locationListenerNetwork = new LocationListener() {

                public void onStatusChanged(String provider, int status, Bundle extras) {

                }

                public void onProviderEnabled(String provider) {

                    Toast.makeText(getApplicationContext(), "gpsenabled", 1000).show();
                }

                public void onProviderDisabled(String provider) {

                    Toast.makeText(getApplicationContext(), "gps disable", 1000).show();
                    get();


                }

                public void onLocationChanged(Location location) {

                    String latitude=String.valueOf(location.getLatitude());
                    String longtitude=String.valueOf(location.getLongitude());
                    //location updated starts here

                }
            };


    if (gps_enabled)
    {
     locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            locatioListnerGps);
     locMan.removeUpdates( locationListenerNetwork);
    }
    if(network_enabled){
     locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
             locationListenerNetwork);
     locMan.removeUpdates( locatioListnerGps);
 }

}
}

请帮助并指导我......

因为我是初学者

提前致谢...

1 个答案:

答案 0 :(得分:0)

if (gps_enabled)
    {
     locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            locatioListnerGps);
     locMan.removeUpdates( locationListenerNetwork);
    }
    if(network_enabled){
     locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
             locationListenerNetwork);
     locMan.removeUpdates( locatioListnerGps);
 }

首先检查您的代码。你为什么要在这里删除更新?

要在BOOT上启动服务,请添加清单:

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

BroadcastReceiver not receiving BOOT_COMPLETED 请点击此链接获取更多信息

试试此代码并告诉我们: 位置更新的服务代码:

public class GPSTracker extends Service implements LocationListener {
private  Context mContext;
private final String TAG = "GPSTracker";

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;
Intent intent = new Intent(MainActivity.LOCATION_CHANGE);

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG,"onStartCommand");
    Log.d(TAG, "flag " + flags + "startId" + startId);
    this.mContext = this;
    getLocation();
    return super.onStartCommand(intent, flags, startId);
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        Log.d(TAG,"getlocation gpsEnabled " + isGPSEnabled + "networkenabled"
                + isNetworkEnabled);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {

                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        Log.d(TAG,"LAtitude :" +latitude +"Lngitude:"+longitude);
                        onLocationChanged(location);

                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            else if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            onLocationChanged(location);
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}
@Override
public void onDestroy() {
    Log.d(TAG,"onDestroy Called");
    stopUsingGPS();
    super.onDestroy();
}
@Override
public void onLocationChanged(Location newLocation) {
    Log.d(TAG,"onLocationChanged new Latitude: " + newLocation.getLatitude() +
            "  \nLongitude :" + newLocation.getLongitude());
    intent.putExtra("latitude", newLocation.getLatitude());
    intent.putExtra("longitude", newLocation.getLongitude());
    sendBroadcast(intent);
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}