Question

Look at code below I need to update this onUpdate method every second, could anybody give some good example, please? Mainly I need to update codes from CurFreq(context); to views.setTextViewText(R.id.text_widget_batt, prefNameBattery+"%");

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            Intent intent = new Intent(context, MyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setOnClickPendingIntent(R.id.layout_widget, pendingIntent);

            CurFreq(context);
            Temp(context);
            Battery(context);

            SharedPreferences myPrefsInfo = context.getSharedPreferences("myPrefsInfo", Context.MODE_PRIVATE);
            String prefNameCurFreq = myPrefsInfo.getString(WidgetCurFreq, "");
            String prefNameTemp = myPrefsInfo.getString(WidgetTemp, "");
            String prefNameBattery = myPrefsInfo.getString(WidgetBattery, "");

            views.setTextViewText(R.id.text_widget_freq, prefNameCurFreq);
            views.setTextViewText(R.id.text_widget_temp, prefNameTemp+",0°C");
            views.setTextViewText(R.id.text_widget_batt, prefNameBattery+"%");

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
}
Was it helpful?

Solution

You can code a Service and implement a timer , and Update the widget every second accordingly. Call your method in OnStartCommand(Intent, int, int) Method of Service with in the timer .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top