gpt4 book ai didi

android - 在 Appwidget 的一个 View 上使用两个 pendingIntent

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:12 26 4
gpt4 key购买 nike

我有一个小部件类(扩展 AppWidgetProvider),它在小部件的布局中只有一个 View (ImageView)。当用户点击小部件时,它会毫无问题地更新和启动 Activity 。此外,小部件每 30 分钟更新一次,并在小部件更新后启动 Activity 。我的问题是:如何将小部件配置为仅在自动更新(而不是通过用户点击)时更新自身?

这是我的代码:

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

最佳答案

更改“remoteView()”方法中的 PendingIntent:

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

您的新“onReceive()”方法可能如下所示:

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

希望这对您有所帮助!

关于android - 在 Appwidget 的一个 View 上使用两个 pendingIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24055983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com