2012-12-04 17 views
5

मेरे पास एक स्थान श्रोता वर्ग और एक सेवा वर्ग है। मेरे स्थान श्रोता वर्ग में मैं जीपीएस शुरू कर रहा हूं और मैं हर 10 सेकंड में स्थान प्राप्त कर रहा हूं। मेरी आवश्यकता है कि हर दिन जीपीएस लोकेशन अपडेट 7 पीएम तक बंद करें। मैं remupdates का उपयोग कर जीपीएस अपडेट को सेवा में रोकने की कोशिश कर रहा हूं लेकिन यह काम नहीं कर रहा है। इसे कैसे प्राप्त किया जा सकता है। नीचे मेरा कोड है।एंड्रॉइड में सेवा कक्षा में स्थान अपडेट को कैसे हटाएं?

LocationGPS.java

public class LocationGPS extends Activity implements LocationListener { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
public static LocationManager locationManager;  
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10*1000 , 0 ,this); 
Intent myIntent = new Intent(this, PIService.class); 
      PendingIntent sevenPMIntent = PendingIntent.getService(this, 0, myIntent, 0); 

      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.set(Calendar.HOUR_OF_DAY, 19); 
      calendar.set(Calendar.MINUTE, 0); 
      calendar.set(Calendar.SECOND, 0); 
      //alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, sevenPMIntent); 

} 

@Override  
public void onLocationChanged(Location location) {  
       Toast.makeText(getApplicationContext(), "got new location", Toast.LENGTH_SHORT).show(); 

}  

@Override 
public void onProviderDisabled(String provider) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

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

}  
} 

PIService.java

public class PIService extends Service{ 

@Override  
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
}   
@Override  
public int onStartCommand (Intent intent, int flags, int startId) 
{  
    super.onStartCommand(intent, flags, startId); 
    //Get the system current time. 
    Date dt = new Date(); 
    int hours = dt.getHours(); 

    //Compare the current time with 7PM 
    if(hours == 19){ 
     //if current time is 7PM 
     Toast.makeText(getApplicationContext(), "Now the time is 7PM", Toast.LENGTH_LONG).show(); 
     LocationGPS.locationManager.removeUpdates(new LocationGPS()); //here is the place i am trying to remove the location updates. 

    } 
    } 


    return START_STICKY;  
} 

} 
+0

इस प्रश्न का संदर्भ यह तुम्हारी मदद करेगा http://stackoverflow.com/questions/ 12458070/android-locationmanager-removeupdateslistener-नहीं-दूर करने-स्थान के श्रोता –

+0

पृष्ठभूमि में लगातार अंतराल में स्थान प्राप्त करने के बजाय, मैं पृष्ठभूमि में स्थान कैसे प्राप्त कर सकता हूं, जबकि स्थान केवल तभी बदला जाता है? – Karthick

उत्तर

2
public void onDestroy() { 
    Log.e(TAG, "onDestroy"); 
    super.onDestroy(); 
    if (mLocationManager != null) { 
     for (int i = 0; i < mLocationListeners.length; i++) { 
      try { 
       mLocationManager.removeUpdates(mLocationListeners[i]); 
      } catch (Exception ex) { 
       Log.i(TAG, "fail to remove location listners, ignore", ex); 
      } 
     } 
    } 
} 
+0

mLocationListeners.length यह बताएं कि श्रोताओं की सूची कैसे प्राप्त करें –

1
public void onDestroy() 
{ 
locationManager.removeUpdates(this); 
super.onDestroy(); 
} 
संबंधित मुद्दे