我正在为 Android 开发一个小部件,它显示您当前的位置。要获取当前的纬度和经度,我有以下代码:
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 的参数中遇到错误。它在 0,0,locationListener 处显示“令牌上的语法错误,应使用 VariableDeclarator” 。
此外,这是一个单独的类。然后我将如何在我的小部件中运行它?就像是:
LocationInfo locationProvider = new LocationInfo();
LocationProvider.run();
也许?再次,任何帮助将不胜感激!