I have googled a lot and tried so many variations but I just can't get my code working. I have a widget that NEEDS to be updated every second but only when the screen is unlocked.
The widget itself should do 2 things:
- set an onClickListener to send the user to my MainActivity
- edit a textbox every second
Since the intervall is really small with one second I thought I have to use an AlarmManger together with an IntentService for the update. Elapsed_Realtime should be right since I don't want to wake up the phone when locked. So this is how my
AppWidgetProvider.onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
looks like:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final Intent intent = new Intent(context, UpdateService.class);
final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),1000, pendingIntent);
}
Do I really need a loop over the widget-IDs here?
This is how my UpdateService looks like:
public class UpdateService extends IntentService {
public static int testInt = 0;
public UpdateService() {
super("stuff UpdateService");
}
@Override
protected void onHandleIntent(Intent intent) {
RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.my_widget);
//stuff
remoteViews.setTextViewText(R.id.textView,String.format("%02d",variableThatChangesEverySecond));
// more stuff
//set onClickListener
final Intent openApp = new Intent(this, MainActivity.class);
final PendingIntent pendingAppIntent = PendingIntent.getActivity(this, 0, openApp, 0);
remoteViews.setOnClickPendingIntent(R.id.myRelativeLayout, pendingAppIntent);
ComponentName thisWidget = new ComponentName(this,myWidget.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
//log a testInt to see how often my widget gets updates
testInt = testInt + 1;
System.out.println(testInt);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
Although I set my intervall 1000ms = 1s my widget updates not exact and only like once in one or two minutes. Here is the logcat to see the updateIntervalls:
06-08 19:21:10.444 19376-19749/mypackage.myapp I/System.out﹕ 1
06-08 19:22:42.900 19376-19798/mypackage.myapp I/System.out﹕ 2
06-08 19:23:02.449 19376-19807/mypackage.myapp I/System.out﹕ 3
06-08 19:24:43.035 19376-19860/mypackage.myapp I/System.out﹕ 4
06-08 19:25:43.087 19376-19876/mypackage.myapp I/System.out﹕ 5
06-08 19:26:43.149 19376-19905/mypackage.myapp I/System.out﹕ 6
06-08 19:27:43.214 19376-19936/mypackage.myapp I/System.out﹕ 7
06-08 19:28:43.282 19376-19960/mypackage.myapp I/System.out﹕ 8
06-08 19:29:43.290 19376-19986/mypackage.myapp I/System.out﹕ 9
06-08 19:30:43.355 19376-20051/mypackage.myapp I/System.out﹕ 10
06-08 19:31:43.419 19376-20096/mypackage.myapp I/System.out﹕ 11
Where is my mistake? Thank you so much, you are my last hope...