2014-01-20 167 views
-1

在我的應用程序重新啓動設備後位置更新停止。 我正在使用gps和網絡來查找我的應用程序中的當前位置,重新啓動後gps工作正常,但是當我關閉gps時,應用程序不幸關閉。 這是我的問題重新啓動設備後位置更新使用NETWORK_PROVIDER停止

這是我的代碼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); 
} 

} 
} 

請幫助和指導我...

因爲我提前beginer

謝謝...

+0

我相信你沒有編碼它重新啓動後自動運行。請訪問此問題[如何在啓動時啓動應用程序?](http://stackoverflow.com/q/6391902/2567598) – Vigbyor

+0

@Vigbyor我有一個代碼,在重新啓動後自動運行。但我的問題不是network_provider在重新啓動後不工作.. – rohit

回答

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

請首先檢查您的代碼。你爲什麼要在這裏刪除更新?

在啓動時自動服務請加在清單:

<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; 
} 
+0

感謝您的回覆。這不是一個問題..在我刪除locMan.removeUpdates(...)代碼後,同樣的問題network_provider重新啓動後無法工作。請指導我... – rohit

+0

你有沒有開始BOOT_complete這項服務.. –

+0

檢查我的編輯答案 –

相關問題