在小部件中实现Android LocationListener
我正在为Android开发一个小部件,它显示您的当前位置。要获取当前纬度和经度,我有以下代码:在小部件中实现Android LocationListener,android,geolocation,widget,Android,Geolocation,Widget,我正在为Android开发一个小部件,它显示您的当前位置。要获取当前纬度和经度,我有以下代码: public class LocationInfo extends Activity { RemoteViews remoteViews; LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); myLocationListener locationListene
public class LocationInfo extends Activity {
RemoteViews remoteViews;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myLocationListener locationListener = new myLocationListener();
LocationProvider locationProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
class myLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
if(location != null)
{
double Long = location.getLongitude();
double Lat = location.getLatitude();
remoteViews.setTextViewText(R.id.widget_textview3, Double.toString(Long));
}
else {
remoteViews.setTextViewText(R.id.widget_textview3, "LOADING...");
}
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}
但是,我在requestLocationUpdates的参数处得到一个错误。它表示“令牌上出现语法错误,应改为使用VariableDeclarator”0,0,locationListener
另外,这是一个单独的类。然后我将如何在我的小部件中运行它?比如:
LocationInfo locationProvider = new LocationInfo();
LocationProvider.run();
也许吧?再次重申,任何帮助都将不胜感激 不允许您执行
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
方法之外。
例如,只需进入活动的创建onCreate
祝你好运好的,谢谢你的回复,但我想我不能在这里使用onCreate,因为我最终要在一个小部件中实现它,对吧?是的。我认为您应该启动一个服务,该服务将监听位置更改并请求小部件更新。在这个更新中,你们将能够用数据填充小部件,这些数据和最后一个已知的位置有关。那个么我就可以这样做了?因为我可以在这个单独的活动中收集信息,然后在我的主文件(小部件)中调用这个活动?如果我在这里错了,你能举个例子说明你的意思吗?我对小部件不是很熟悉,但是,我认为它不是为了从活动中操纵它们,当然你不能在活动中存储对它们的引用,因为它可以随时被系统破坏,但小部件-不是。看看这个-。据我所知,您应该提供一个由系统触发的提供程序,它将使用RemoveView界面操作小部件。你打算这样做吗?是的,我的想法是为小部件提供一个提供者,在这个提供者中,我打电话给LocationInfo活动。然后在LocationInfo活动中,我收集信息并使用RemoteView显示它。所以基本上,我告诉提供者运行活动,所有的工作都在活动中完成。但我不完全确定我能做到这一点,也不确定我将如何做到这一点。。