Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use two pendingIntent on one view in Appwidget

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();
}

}

like image 318
RuNo280 Avatar asked Jun 05 '14 09:06

RuNo280


1 Answers

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!

like image 134
Bö macht Blau Avatar answered Sep 22 '22 20:09

Bö macht Blau