【发布时间】:2013-04-11 07:05:00
【问题描述】:
我有实现位置监听器的服务。现在我的问题是如何确保我的服务即使在睡眠模式下也能捕获位置。我已阅读有关警报管理器的信息
alarm.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, intervalMillis, operation);
但是如何使用它。这是我的代码..任何帮助将不胜感激..
我的服务
public class LocationCaptureService extends Service implements LocationListener {
public static int inteval;
java.sql.Timestamp createdTime;
LocationManager LocationMngr;
@Override
public void onCreate() {
inteval=10*1000;
startLocationListener(inteval);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
private void startLocationListener(int inteval,String nwProvider) {
this.LocationMngr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
this.LocationMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, inteval, 0, this);
}
public void onLocationChanged(Location location) {
String status="c",S=null;
double longitude,lattitude,altitude;
g_currentBestLocation = location;
createdTime = new Timestamp (new java.util.Date().getTime());
longitude=location.getLongitude();
lattitude=location.getLatitude();
altitude=location.getAltitude();
//use this
}
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
【问题讨论】:
-
我认为您不想使用
AlarmManager,除非您需要安排特定的时间间隔来查询位置。如果您希望在 a) 您的应用完成 b) 用户告诉您的应用停止之前不要终止您的服务,那么您需要一种不同的方法。 -
thanx for the comment.. 你能告诉我如何在我的应用程序完成之前让服务不被杀死。我需要返回 start_sticky