In one requirement I have created one widget that should be updated after every 5 minutes. I found on internet that instead of using java thread I should use AlarmManager that will save the battery. Now I am doing it using AlarmManager.
The problem I am facing is the onStart method of Service class is getting called very very frequently. Almost 10 times in a second. Hear is my two classes.
public class MyAppWidget extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
intent = new Intent(context, UpdateWidgetService.class);
context.startService(intent);
}
}
The Service class is
public class UpdateWidgetService extends Service {
private Intent intent;
@Override
public void onStart(Intent intent, int startId) {
this.intent = intent;
System.out.println("This is getting printed 10 times in a second.");
final PendingIntent pending = PendingIntent.getService(getApplication(), 0, intent, 0);
AlarmManager alarm = (AlarmManager) getApplication().getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
long interval = 3000;
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);
stopSelf();
super.onStart(intent, startId);
}
}
I've also set property in xml file for auto refresh but it is no use. The app is not using the below property to refresh the widget. Also I wanted to know which method will be called if this property is used by the application.
android:updatePeriodMillis="1000"