위치 수신기 클래스와 서비스 클래스가 있습니다. 내 위치 수신기 클래스에서 GPS를 시작하고 10 초마다 위치를 계속 유지합니다. 내 요구 사항은 매일 오후 7 시까 지 GPS 위치 업데이트를 중지하는 것입니다. removeupdates를 사용하여 서비스중인 GPS 업데이트를 중지하려고하지만 작동하지 않습니다. 나는 어떻게 이것을 달성 할 수 있는가. 아래는 제 코드입니다.안드로이드에서 서비스 클래스의 위치 업데이트를 제거하는 방법은 무엇입니까?
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;
}
}
이 질문을 참조하십시오이 당신에게 도움이 될 것입니다 http://stackoverflow.com/questions/ 12458070/android-locationmanager-removeupdateslistener-not-removing-location- 청취자 –
빈번한 간격으로 백그라운드에서 위치를 얻는 대신, 위치가 변경되는 동안 백그라운드에서 위치를 얻으려면 어떻게해야합니까? – Karthick