我有以下代码,它是一个服务,可以找到设备的当前位置并将其发送到网络服务。
该服务从推送通知(firebase)启动。然后,接收推送的firebase类启动一个IntentService,然后调用此普通Android服务的位置。
我必须使用IntentService来启动服务,因为我的应用程序还会处理另外500次不同的推送,以执行各种功能,包括Web调用。
我遇到的问题是在某些设备上,例如三星galaxy s6,一旦定位服务开始,它每2秒重复获得一个新锁....永远。 (即使我打电话给自己服务)。在我的S7上,它获得一次锁定,一切都很顺利。 s6是android 6,s7是Android 7.
我注意到我调用以下行开始寻找更新,传递时间和距离间隔为0,0。我应该将它们设置为更高的值吗?
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener, looper);
如果这不是问题,我知道我应该使用Google Play服务位置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
答案 0 :(得分:1)
如果您只想进行一次更新,请使用requestSingleUpdate。它的意思是只开一次。