在窗口小部件中实现Android LocationListener

时间:2012-09-03 07:06:18

标签: android geolocation widget

我正在开发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的参数中收到错误。它表示“令牌上的语法错误,而而是预期的VariableDeclarator”位于0,0,locationListener。

此外,这是一个单独的类。我怎么会在我的小部件中运行它?类似的东西:

LocationInfo locationProvider = new LocationInfo();
LocationProvider.run();

可能?再次,真的很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

您不得执行

locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

方法之外。 只需移动到例如onCreate的Activity。

祝你好运