I am working on a widget for Android, which displays your current location. To get the current latitude and longitude I have the following code:
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
}
}
}
However, I get an error at the arguments of requestLocationUpdates. It says “Syntax error on tokens, VariableDeclarator expected instead” at 0,0, locationListener.
Also, this is a separate class. How would I then run this in my widget? Something like:
LocationInfo locationProvider = new LocationInfo();
LocationProvider.run();
maybe? Again, any help would really be appreciated!
You are not allowed to execute
outside of methods.
Just move in to, for example,
onCreateof Activity.Good luck