Android LocationListener 如何返回数据
我想构建一个应用程序,用户可以在其中制作标有手机当前位置的照片。因此,制作完照片后,用户可以通过触摸“保存”按钮来保存图片。如果触摸按钮,当前位置将由我自己的 LocationListener 类确定。 Listener 类显示一个进度对话框,并在找到位置后将其关闭。但现在我想知道我可以将位置返回给调用活动,因为位置侦听器方法是回调方法。是否有一个“最佳实践”解决方案,或者有人有线索吗?
位置监听器:
public class MyLocationListener implements LocationListener {
private ProgressDialog progressDialog;
private Context mContext;
private LocationManager locationManager;
public MyLocationListener(Context context, ProgressDialog dialog) {
mContext = context;
progressDialog = dialog;
}
public void startTracking() {
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
String provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 10, 10, this);
progressDialog.show();
}
private void finishTracking(Location location) {
if(location != null) {
locationManager.removeUpdates(this);
progressDialog.hide();
Log.i("TRACKING",location.toString());
}
}
@Override
public void onLocationChanged(Location location) {
finishTracking(location);
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
}
调用代码:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Determine position...");
new MyLocationListener(this, dialog).startTracking();
i want to build an app in which users can make foto which is tagged with the current location of the phone. So after making a foto, the user can save the picture by touching on the "save" button. If the button is touched the current location will be determined with my own LocationListener class. The Listener-class shows a Progressdialog and dismiss it, after a location is found. But now i want to know which i can return the location back to the calling Activity, because the Location listener methods are callback methods. Is there a "best-practice" solution for that, or have anybody a clue?
Location Listener:
public class MyLocationListener implements LocationListener {
private ProgressDialog progressDialog;
private Context mContext;
private LocationManager locationManager;
public MyLocationListener(Context context, ProgressDialog dialog) {
mContext = context;
progressDialog = dialog;
}
public void startTracking() {
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
String provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 10, 10, this);
progressDialog.show();
}
private void finishTracking(Location location) {
if(location != null) {
locationManager.removeUpdates(this);
progressDialog.hide();
Log.i("TRACKING",location.toString());
}
}
@Override
public void onLocationChanged(Location location) {
finishTracking(location);
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
}
Calling code:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Determine position...");
new MyLocationListener(this, dialog).startTracking();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不将您自己的回调从 Activity 传递到 MyLocationListener 并从 finishTracking 调用其方法?
它是一种常用的委托模式。类似这样的事情:
并调用:
Why not pass your own callback from activity to MyLocationListener and call its method from finishTracking?
Its a commonly used delegation pattern. Something like that:
and call:
您可以做另一件简单的事情 - 将您的类
MyLocationListener
放入您的Activity
本身中,并修改MyLocationListener
本身内活动中的字段。You can do another simple thing - put your class
MyLocationListener
inside yourActivity
itself and modify the field in your activity inside yourMyLocationListener
itself.