-1

In my application Location update stops after reboot the device. i'm using both gps and network to find a current location in my application,after reboot gps is working correctly but when i turn off the gps the app is unfortunately closed. this is my problem

this is my code 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);
 }

}
}

please help and guide me...

because i'm beginer

Thanks in advance...

rohit
  • 113
  • 2
  • 7
  • I believe you haven't code it to auto run after re-boot. Visit this question [How to Start an Application on Startup?](http://stackoverflow.com/q/6391902/2567598) – Vigbyor Jan 20 '14 at 11:37
  • @Vigbyor i have a code to auto run after re-boot.but my question is not that network_provider is not working after reboot thats all.. – rohit Jan 20 '14 at 11:40

1 Answers1

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

Check your code first. Why are you removing updates here?

To start a service on BOOT please add in manifest:

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

BroadcastReceiver not receiving BOOT_COMPLETED Chk this link for more info

Try this code and let me know : Service code for location update :

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;
}
Community
  • 1
  • 1
Zohra Khan
  • 5,024
  • 4
  • 24
  • 34
  • thanks for your responce. that's not a problem.. after i remove the locMan.removeUpdates(...) code the same problem network_provider is not working after reboot. please guide me... – rohit Jan 20 '14 at 11:54
  • yes.. i start the service and then the gps location update is working correctly – rohit Jan 20 '14 at 11:59
  • i done all of these correctly and reboot service is starting correctly and gps location is updated perfectly... when i turn off the gps the app is unfortunately stoped. thats only my problem – rohit Jan 20 '14 at 12:03
  • Broadcast.check==false ..what is this flag? – Zohra Khan Jan 20 '14 at 12:04
  • this flag is.. i use here... public class Broadcast extends BroadcastReceiver { public static boolean check=true; @Override public void onReceive(Context content, Intent intent) { check=false; Intent serviceIntent = new Intent("com.example.Servicestart"); content.startService(serviceIntent); } } – rohit Jan 20 '14 at 12:06
  • how are you getting location update from service in activity? – Zohra Khan Jan 20 '14 at 12:07
  • the location,just i store to database – rohit Jan 20 '14 at 12:09
  • ok, But how your activity will know that there is a change in database? – Zohra Khan Jan 20 '14 at 12:10
  • i'm not understand clearly...i have a json class . in my servicestart.java file i get the location and pass as a parameter to thejson class here i update the location to database using php-mysql interaction – rohit Jan 20 '14 at 12:15