I am working on a widget that will receive the current GPS location and transfer this value to a remote PHP page in order to get the information and display it in the Widget. This is what I am trying to do.
I am facing a problem when implementing the Location Listener for appWidget. It is not updated by the current location and shows the initial widget ie "Download widget" (here I put this text)
Can we implement a location receiver for AppWidgetProvider?
Or
Can you suggest me a possible GPS location solution in the App Widget?
I placed all the necessary permissions in the manifest file.
Here is my code snippet:
public class test extends AppWidgetProvider {
static LocationManager locMan;
static Location curLocation;
static Boolean locationChanged;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
context.startService(new Intent(context, UpdateService.class));
}
public static class UpdateService extends Service {
LocationListener gpsListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (curLocation == null) {
curLocation = location;
locationChanged = true;
}
if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude())
locationChanged = false;
else
locationChanged = true;
curLocation = location;
if (locationChanged)
locMan.removeUpdates(gpsListener);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
if (status == 0)
{
} else if (status == 1)
{
} else if (status == 2) {
}
}
};
@Override
public void onStart(Intent intent, int startId) {
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000, 1, gpsListener);
} else {
this.startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
}
if (curLocation != null) {
double lat = curLocation.getLatitude();
double lng = curLocation.getLongitude();
Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show();
}
RemoteViews updateViews = buildUpdate(this);
ComponentName thisWidget = new ComponentName(this,InKakinadaWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
public RemoteViews buildUpdate(Context context) {
return updateViews;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
}
code>
Thanks at Advance ...
,
.