10

I have a widget class (extending AppWidgetProvider), which has only one view (ImageView) in the widget's layout. When the user taps on widget, it updates and launches an activity with no problem. Also the widget updates every 30 minutes and activity launch after widget update. My problem is: how can I configure the widget to only update itself when updated automatically (not by user click)?

Here is my code:

public class Widget extends AppWidgetProvider {

private static final String ACTION_UPDATE = AppWidgetManager.ACTION_APPWIDGET_UPDATE;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    ComponentName cn = new ComponentName(context, Widget.class);
    appWidgetManager.updateAppWidget(cn, remoteView(context));
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (intent.getAction().equalsIgnoreCase(ACTION_UPDATE)) {
        ComponentName cn = new ComponentName(context, Widget.class);
        AppWidgetManager.getInstance(context).updateAppWidget(cn,
                remoteView(context));
        Intent launch = new Intent(context, Main.class);
        PendingIntent pi = PendingIntent.getActivity(context, 0, launch, 0);
        try {
            pi.send();
        } catch (CanceledException e) {
            e.printStackTrace();
        }
    }
}

private RemoteViews remoteView(Context ctx) {
    ArrayList<Integer> imageId = new ArrayList<Integer>();
    for (int i = 1; i <= 30; i++) {
        int drawableImageId = ctx.getResources().getIdentifier("image" + i,
                "drawable", ctx.getPackageName());
        imageId.add(drawableImageId);
    }

    RemoteViews rv = new RemoteViews(ctx.getPackageName(), R.layout.widget);

    Calendar todayDate = Calendar.getInstance();
    todayDate.setTimeInMillis(System.currentTimeMillis());

    Calendar startDate = Calendar.getInstance();
    startDate.set(2014, 05, 15);

    int dayDiffer = Helper.getDiffernce(todayDate, startDate);
    dayDiffer += 1;

    if (dayDiffer >= 1 && dayDiffer <= 30) {
        rv.setInt(R.id.img_widget, "setImageResource",
                imageId.get(dayDiffer - 1));
        T(ctx, "in  >> " + dayDiffer);
    } else if (dayDiffer >= 0) {
        T(ctx, "before  >> " + dayDiffer);
    } else if (dayDiffer <= 31) {
        T(ctx, "after  >> " + dayDiffer);
    }

    Intent update = new Intent(ctx, Widget.class);
    update.setAction(ACTION_UPDATE);
    PendingIntent pi_update = PendingIntent.getBroadcast(ctx, 0, update, 0);
    rv.setOnClickPendingIntent(R.id.img_widget, pi_update);

    return rv;
}

public void T(Context context, String string) {
    Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
}

}

nkorth
  • 1,684
  • 1
  • 12
  • 28
RuNo280
  • 573
  • 4
  • 27

2 Answers2

1

Change the PendingIntent in your "remoteView()" method:

Intent userInput = new Intent(ctx, Widget.class);
userInput.setAction("LAUNCH_MAIN_ACTIVITY");    // use another action!

PendingIntent pi_userInput = 
                   PendingIntent.getBroadcast(ctx, 0, userInput, 0);
rv.setOnClickPendingIntent(R.id.img_widget, pi_userInput);

Your new "onReceive()" method could look like this:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    if (intent.getAction().equalsIgnoreCase(ACTION_UPDATE)) {
        ComponentName cn = new ComponentName(context, Widget.class);
        AppWidgetManager.getInstance(context).updateAppWidget(cn,
                remoteView(context));

    }

    // the launch will always be executed,
    // the update only for "ACTION_UPDATE" 
    Intent launch = new Intent(context, Main.class);
    PendingIntent pi = PendingIntent.getActivity(context, 0, launch, 0);
    try {
        pi.send();
    } catch (CanceledException e) {
        e.printStackTrace();
    }
}

Hope this helps!

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
0

you can use android service for update or start activity automatic

er reflex
  • 82
  • 9
  • thank you , but its very simple widget and should stay simple , I think there is a way ... – RuNo280 Jun 05 '14 at 09:35
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Balaji Kandasamy Jun 05 '14 at 09:53