Question

I am working on android appwidget , There are issues updating appwidget periodically . Changed updating period to more than 30 minutes . Still not updating the widget

public class MyWidgetProvider extends AppWidgetProvider {

final static String ITEM_CLICK_ACTION = "ItemClick";
public static final String TOAST_ACTION = "TOAST_ACTION";

BroadcastReceiver receiver;

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];

        Log.e("called", "omupdate");

        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget_lay);

        views.setTextViewText(R.id.textView1,
                "Floating Text will be displayed here .. Daily Thoughts");

        Intent intent = new Intent(context, RemoteServiceEmin.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        // intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        views.setRemoteAdapter(R.id.gridview, intent);

        // inflating list remote view

        Intent intentlist = new Intent(context, ListInflater.class);
        intentlist.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetId);
        // intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        views.setRemoteAdapter(R.id.listview, intentlist);

        // setting onclick on gridview
        Intent itemClickIntent = new Intent(context, MyWidgetProvider.class);
        itemClickIntent.setAction(ITEM_CLICK_ACTION);
        itemClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetIds[i]);
        PendingIntent itemClickPendingIntent = PendingIntent.getBroadcast(
                context, 0, itemClickIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.gridview,
                itemClickPendingIntent);

        Intent toastIntent = new Intent(context, MyWidgetProvider.class);
        toastIntent.setAction(MyWidgetProvider.TOAST_ACTION);
        toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetIds[i]);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent toastPendingIntent = PendingIntent.getBroadcast(
                context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setPendingIntentTemplate(R.id.ll, toastPendingIntent);

        // appWidgetManager.updateAppWidget(appWidgetIds[i], v);
        // appWidgetManager.updateAppWidget(appWidgetIds[i], views);
        appWidgetManager.updateAppWidget(appWidgetId, views);

    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);

}

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    if (intent.getAction().equals(ITEM_CLICK_ACTION)) {
        Bundle extras = new Bundle();
        extras.putString("text", intent.getExtras().getString("text"));
        Intent actIntent = new Intent();
        actIntent.setClass(context, ConfigurationActivity.class);
        actIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        actIntent.putExtras(extras);
        context.startActivity(actIntent);
    } else if (intent.getAction().equals(TOAST_ACTION)) {
        Toast.makeText(context, "dfjdsfl", Toast.LENGTH_SHORT).show();

    } else {
        super.onReceive(context, intent);
    }

}

}

this is My appwidget class, i can't track why isnt it updating after android:updatePeriodMillis="50000"

thanks in advance ..

Was it helpful?

Solution

the minimum actual time for widget update is 30 minutes to avoid poor programmed widgets to dry battery. For frequent update of widget use AlarmManager to overcome this .

  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds){

     long interval = 50000;
    Intent in = new Intent(context, Widgetclass.class);
in.setAction("Widgetupdate");
    PendingIntent pi = PendingIntent.getService(context, 0, in, 0);

    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);


        alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pi);
//add remaining code here
}
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("widgetupdate")) {
watchWidget = new ComponentName(context,
            Widgetclass.class);
 RemoteViews views = new RemoteViews(context.getPackageName(),
            R.layout.widget_lay);
(AppWidgetManager.getInstance(context)).updateAppWidget(
                    watchWidget, views);
}
declare this  receiver action in menifest file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top