0

我有一个布局有两个按钮的小部件。我的代码如下,

public void onReceive(Context context, Intent i) {

    AppWidgetManager mgr = AppWidgetManager.getInstance(context);

    Intent intent = new Intent(context, Activity1.class);
    PendingIntent pendingLayout = PendingIntent.getActivity(context, 0, intent, 0);


    Intent searchIntent = new Intent(context, Activity2.class);
    PendingIntent searchPendingLayout = PendingIntent.getActivity(context, 0, searchIntent, 0);

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

    views.setOnClickPendingIntent(R.id.widgetbutton, pendingLayout);
    views.setOnClickPendingIntent(R.id.widgetsearch, searchPendingLayout);

    ComponentName comp = new ComponentName(context,
            RecentTaskWidget.class.getName());

    mgr.updateAppWidget(comp, views);
}

widgetbutton 和 widgetsearch 是布局中的两个视图。我从 remoteviews 对象中获取相应的视图,并将挂起的布局添加到每个视图中。但是,活动尚未开始。代码有什么问题吗?

谢谢!

4

1 回答 1

0

覆盖onUpdate()您的方法AppWidgetProvider并将此代码添加到它的主体:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    AppWidgetManager mgr = AppWidgetManager.getInstance(context);
    Intent intent = new Intent(context, Activity1.class);
    PendingIntent pendingLayout = PendingIntent.getActivity(context, 0, intent, 0);
    Intent searchIntent = new Intent(context, Activity2.class);
    PendingIntent searchPendingLayout = PendingIntent.getActivity(context, 0, searchIntent, 0);
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.searchwidget);
    views.setOnClickPendingIntent(R.id.widgetbutton, pendingLayout);
    views.setOnClickPendingIntent(R.id.widgetsearch, searchPendingLayout);
    ...
    appWidgetManager.updateAppWidget(appWidgetIds, views);
}       

您可以查看此页面以获取更多详细信息。

于 2012-07-27T13:44:18.030 回答