位置更新每 2 秒触发一次
Location update fires avery 2 seconds
我有以下代码,它是一个服务,可以找到设备的当前位置并将其发送到网络服务。
该服务是从推送通知 (firebase) 启动的。接收推送的 firebase class 然后启动一个 IntentService,后者又调用这个普通的 Android 服务,位置在.
我必须使用 IntentService 来启动服务,因为我的应用程序还处理另外 500 次不同的推送以执行各种功能,其中可能包括网络调用。
我遇到的问题是在某些设备上,例如 samsung galaxy s6,一旦定位服务启动,它就会每 2 秒重复获得一个新锁....永远。 (即使我在服务上打电话给 stopself)。在我的 S7 上,它被锁定一次,一切都很好。 s6 是 android 6,s7 是 Android 7.
我注意到我调用以下行开始查找更新,为时间和距离间隔传入 0,0。我应该将它们设置为更高的值吗?
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener, looper);
如果这不是问题,我知道我应该使用 Google Play Services Location API。
Google Play Services Location APIs
但我找不到任何教程告诉我如何在服务中使用这些 api 来只找到一次位置。
有没有人知道为什么位置更新仅在某些手机上每 2 秒触发一次?
public class TrackingService extends Service {
private static final String TAG = TrackingService.class.getSimpleName();
LocationManager mlocManager;
LocationListener mlocListener;
AppObj appObj;
Handler endServiceHandler;
Runnable endServiceRunnable;
HandlerThread handlerThread;
Looper looper;
Location mLoc;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "inside tracking service onStartCommand");
appObj = (AppObj) getApplication();
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
Log.e(TAG, "Service created and location manager and listener created");
Log.e(TAG, "creating handlerthread and looper");
handlerThread = new HandlerThread("MyHandlerThread");
handlerThread.start();
looper = handlerThread.getLooper();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener, looper);
Log.e(TAG, "requesting location updates");
killLocationRequestHandler();
endServiceHandler.postDelayed(endServiceRunnable, 45 * 1000);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "in onDestroy in LocationService class");
try {
mlocManager.removeUpdates(mlocListener);
}catch(Exception e){}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
mLoc = loc;
Log.e(TAG, "in TrackingService onlocationChanged and about to send lat/lon " + loc.getLatitude() + " " + loc.getLongitude());
DateTime dt = new DateTime(DateTimeZone.UTC);
DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS").withLocale(Locale.UK).withOffsetParsed();
String formattedNowTime3 = df3.print(dt);
Log.e(TAG, "Time of location fix in TrackingService = " + formattedNowTime3);
appObj.webService.sendUserLocation(formattedNowTime3, String.valueOf(loc.getLatitude()), String.valueOf(loc.getLongitude()));
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "co-ods = " + String.valueOf(mLoc.getLatitude())
+ " " + String.valueOf(mLoc.getLongitude()), Toast.LENGTH_LONG).show();
}
});
Log.e(TAG, "quiting handlerthread");
handlerThread.quit();
try {
mlocManager.removeUpdates(mlocListener);
}catch(Exception e){}
Log.e(TAG, "removed updates(TrackingService)");
TrackingService.this.stopSelf();
Log.e(TAG, "called stopSelf on TrackingService");
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
public void killLocationRequestHandler() {
endServiceHandler = new Handler();
endServiceRunnable = new Runnable() {
public void run() {
endService();
}
private void endService() {
try {
mlocManager.removeUpdates(mlocListener);
}catch(Exception e){}
Log.e(TAG, "removed updates(TrackingService) from the endService handler");
DateTime dt = new DateTime();
DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
String formattedNowTime3 = df3.print(dt);
Log.e(TAG, "Time of location fix in TrackingService = " + formattedNowTime3);
appObj.webService.sendUserLocation(formattedNowTime3, "NO_LAT", "NO_LON");
TrackingService.this.stopSelf();
Log.e(TAG, "called stopSelf on TrackingService from the endService handler");
}
};
}
}// end of service
如果您只需要一个更新,请使用 requestSingleUpdate。它意味着只发射一次。
我有以下代码,它是一个服务,可以找到设备的当前位置并将其发送到网络服务。
该服务是从推送通知 (firebase) 启动的。接收推送的 firebase class 然后启动一个 IntentService,后者又调用这个普通的 Android 服务,位置在.
我必须使用 IntentService 来启动服务,因为我的应用程序还处理另外 500 次不同的推送以执行各种功能,其中可能包括网络调用。
我遇到的问题是在某些设备上,例如 samsung galaxy s6,一旦定位服务启动,它就会每 2 秒重复获得一个新锁....永远。 (即使我在服务上打电话给 stopself)。在我的 S7 上,它被锁定一次,一切都很好。 s6 是 android 6,s7 是 Android 7.
我注意到我调用以下行开始查找更新,为时间和距离间隔传入 0,0。我应该将它们设置为更高的值吗?
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener, looper);
如果这不是问题,我知道我应该使用 Google Play Services Location API。
Google Play Services Location APIs
但我找不到任何教程告诉我如何在服务中使用这些 api 来只找到一次位置。
有没有人知道为什么位置更新仅在某些手机上每 2 秒触发一次?
public class TrackingService extends Service {
private static final String TAG = TrackingService.class.getSimpleName();
LocationManager mlocManager;
LocationListener mlocListener;
AppObj appObj;
Handler endServiceHandler;
Runnable endServiceRunnable;
HandlerThread handlerThread;
Looper looper;
Location mLoc;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "inside tracking service onStartCommand");
appObj = (AppObj) getApplication();
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
Log.e(TAG, "Service created and location manager and listener created");
Log.e(TAG, "creating handlerthread and looper");
handlerThread = new HandlerThread("MyHandlerThread");
handlerThread.start();
looper = handlerThread.getLooper();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener, looper);
Log.e(TAG, "requesting location updates");
killLocationRequestHandler();
endServiceHandler.postDelayed(endServiceRunnable, 45 * 1000);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "in onDestroy in LocationService class");
try {
mlocManager.removeUpdates(mlocListener);
}catch(Exception e){}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
mLoc = loc;
Log.e(TAG, "in TrackingService onlocationChanged and about to send lat/lon " + loc.getLatitude() + " " + loc.getLongitude());
DateTime dt = new DateTime(DateTimeZone.UTC);
DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS").withLocale(Locale.UK).withOffsetParsed();
String formattedNowTime3 = df3.print(dt);
Log.e(TAG, "Time of location fix in TrackingService = " + formattedNowTime3);
appObj.webService.sendUserLocation(formattedNowTime3, String.valueOf(loc.getLatitude()), String.valueOf(loc.getLongitude()));
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "co-ods = " + String.valueOf(mLoc.getLatitude())
+ " " + String.valueOf(mLoc.getLongitude()), Toast.LENGTH_LONG).show();
}
});
Log.e(TAG, "quiting handlerthread");
handlerThread.quit();
try {
mlocManager.removeUpdates(mlocListener);
}catch(Exception e){}
Log.e(TAG, "removed updates(TrackingService)");
TrackingService.this.stopSelf();
Log.e(TAG, "called stopSelf on TrackingService");
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
public void killLocationRequestHandler() {
endServiceHandler = new Handler();
endServiceRunnable = new Runnable() {
public void run() {
endService();
}
private void endService() {
try {
mlocManager.removeUpdates(mlocListener);
}catch(Exception e){}
Log.e(TAG, "removed updates(TrackingService) from the endService handler");
DateTime dt = new DateTime();
DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
String formattedNowTime3 = df3.print(dt);
Log.e(TAG, "Time of location fix in TrackingService = " + formattedNowTime3);
appObj.webService.sendUserLocation(formattedNowTime3, "NO_LAT", "NO_LON");
TrackingService.this.stopSelf();
Log.e(TAG, "called stopSelf on TrackingService from the endService handler");
}
};
}
}// end of service
如果您只需要一个更新,请使用 requestSingleUpdate。它意味着只发射一次。