Android 安卓将服务或应用程序类用于locationListener
我的任务是确定用户位置。我可以在应用程序类中创建LocationListener。或者我可以使用服务Android 安卓将服务或应用程序类用于locationListener,android,service,location,Android,Service,Location,我的任务是确定用户位置。我可以在应用程序类中创建LocationListener。或者我可以使用服务 LocationManager locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener=new LocationListener(){ public void onLocationChanged( Locatio
LocationManager locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener=new LocationListener(){
public void onLocationChanged( Location location){
dive.setLongitude(location.getLongitude());
dive.setLatitude(location.getLatitude());
mapHelper.setMapPosition(dive.getLatitude(),dive.getLongitude());
}
public void onStatusChanged( String provider, int status, Bundle extras){
}
public void onProviderEnabled( String provider){
}
public void onProviderDisabled( String provider){
}};
locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,0,0,locationListener);
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,0,0,locationListener);
此任务需要使用什么?服务类还是应用类?这项服务的优点和缺点是什么?应用程序类的优点和缺点是什么?您永远不会使用该应用程序。您可以使用该活动。使用该活动更方便,但不能持续到下一个活动。该服务需要额外的样板文件,数据访问也不太方便,但它可以用于需要数据的多个活动。如果您的应用程序需要获取实时位置信息,则两者都可以使用
MyApplication extends Application implements locationListener
MyService extends Service implements locationListener
因为应用程序的生命周期最长,所以您可以在服务中使用该应用程序。您只需要关心如何将获取的数据传递给活动(接口、广播…。“您从未使用过该应用程序”-为什么?因为您不应该将其用于任何用途。在安卓系统中这是一个死主意。在某一点上,人们使用它来保存static(单例等),但是子类(您必须记住在清单中重写它)让人头疼,而且与在其他地方使用它相比,它实际上没有任何好处。现在它实际上只用于1或2个系统调用,比如几乎没有人覆盖的onLowMemory。它有点像你的阑尾——它曾经有一个目的,但现在已经没有了。