Хотел сделать виджет в котором считывались бы текущие координаты GPS через определенное время и обновлялось бы информация на экране. Но что-то пошло не так. Сделал настройку, что бы виджет сам обновлялся очень часто. Но координаты GPS получаю только один, два раза и все затихает. Не могу понять почему так происходит?
Код: Выделить всё
package com.example.wiget004;
public class Widget004Test extends AppWidgetProvider {
final String UPDATE_ALL_WIDGETS = "update_all_widgets";
private static String text01 = "---";
private LocationManager locationManager;
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Log.d("myLogs", "updateAppWidget");
RemoteViews widgetView = new RemoteViews(context.getPackageName(),
R.layout.widget004_test);
widgetView.setTextViewText(R.id.appwidget_text, text01);
appWidgetManager.updateAppWidget(appWidgetId, widgetView);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.d("myLogs", "onUpdate");
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Log.d("myLogs", "onDeleted");
for (int appWidgetId : appWidgetIds) {
Widget004TestConfigureActivity.deleteTitlePref(context, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 5, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 6000, 5, locationListener);
Log.d("myLogs", "onEnabled");
Intent intent = new Intent(context, Widget004Test.class);
intent.setAction(UPDATE_ALL_WIDGETS);
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
6000, pIntent);
}
@Override
public void onDisabled(Context context) {
Log.d("myLogs", "onDisabled");
Intent intent = new Intent(context, Widget004Test.class);
intent.setAction(UPDATE_ALL_WIDGETS);
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pIntent);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.d("myLogs", "onReceive");
if (locationManager != null) {
Log.d("myLogs", "locationManager != null");
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Log.d("myLogs", "location != null gps");
text01= "Широта="+String.format("%1$.4f" ,location.getLatitude())+" Долгота="+String.format("%1$.4f",location.getLongitude());
Log.d("myLogs", "getLastKnownLocation="+text01);
}
Location location2 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location2 != null) {
Log.d("myLogs", "location != null net");
text01= "Широта="+String.format("%1$.4f" ,location2.getLatitude())+" Долгота="+String.format("%1$.4f",location.getLongitude());
Log.d("myLogs", "getLastKnownLocation="+text01);
}
}
if (intent.getAction().equalsIgnoreCase(UPDATE_ALL_WIDGETS)) {
ComponentName thisAppWidget = new ComponentName(
context.getPackageName(), getClass().getName());
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
for (int appWidgetID : ids) {
updateAppWidget(context, appWidgetManager, appWidgetID);
}
}
}
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d("myLogs", "onLocationChanged");
showLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
Log.d("myLogs", "onProviderDisabled");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("myLogs", "onProviderEnabled");
showLocation(locationManager.getLastKnownLocation(provider));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("myLogs","onStatusChanged");
if (provider.equals(LocationManager.GPS_PROVIDER)) {
Log.d("myLogs","Status GPS_PROVIDER: " + String.valueOf(status));
} else if (provider.equals(LocationManager.NETWORK_PROVIDER)) {
Log.d("myLogs","Status NETWORK_PROVIDER: " + String.valueOf(status));
}
}
private void showLocation(Location location) {
Log.d("myLogs","showLocation");
if (location == null)
return;
if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
text01= "Широта="+String.format("%1$.4f" ,location.getLatitude())+" Долгота="+String.format("%1$.4f",location.getLongitude());
Log.d("myLogs", text01);
} else if (location.getProvider().equals(
LocationManager.NETWORK_PROVIDER)) {
text01= "Широта="+String.format("%1$.4f" ,location.getLatitude())+" Долгота="+String.format("%1$.4f",location.getLongitude());
Log.d("myLogs", text01);
}
}
};
}