android - 如何删除android中服务类中的位置更新?

标签 android service gps locationlistener

我有一个位置监听器类和一个服务类。在我的位置监听器类中,我正在启动 GPS,并且每 10 秒就会不断获取位置信息。我的要求是每天晚上 7 点之前停止 GPS 位置更新。我正在尝试使用 removeupdates 停止服务中的 GPS 更新,但它不起作用。我怎样才能做到这一点。下面是我的代码。

定位GPS.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;      
}  

}   

最佳答案

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

关于android - 如何删除android中服务类中的位置更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13695994/

相关文章:

android - stopSelf() vs stopSelf(int) vs stopService(Intent)

android - 为 cocos2dx Android 设置 NDK_MODULE_PATH

android - 按钮 onClick 在 vi​​ewholder 模式 ListView 中的位置错误

android - 应用程序上下文被杀死但 Activity 没有

python - 使用 Python 创建用户轨迹

time - GPS时钟的精度如何?

java - 启用 GPS 时启动我的 Android 应用程序强制关闭

android - Glide 库中的 BitmapTransformation 未按预期工作

java - Android在Service类中使用数据库

forms - Symfony2 : Difference between those metodology "Embedded Form" and "Data Transformer"