Android小部件:按钮单击小部件时的方法调用
因为我是android小部件的新手,所以我在调用my方法时遇到了麻烦,该方法用于从我的Appwidget执行web服务数据收集。这是我的密码Android小部件:按钮单击小部件时的方法调用,android,android-widget,Android,Android Widget,因为我是android小部件的新手,所以我在调用my方法时遇到了麻烦,该方法用于从我的Appwidget执行web服务数据收集。这是我的密码 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { this.context=context; RemoteViews remoteViews = new RemoteViews(cont
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
this.context=context;
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
// LAUNCHING PENDING INTENTS FOR CLICKS
//remoteViews.setOnClickPendingIntent(R.id.wid_refresh, pendingMsgIntent);
remoteViews.setOnClickPendingIntent(R.id.widget_button, getServerData(context));
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
按钮按下(上下文)是我的方法。定义为
protected PendingIntent buttonPressed(Context context) {
Log.i("buttonPressed","called");
Intent active = new Intent(context, MainWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
return PendingIntent.getBroadcast(context, 0, active, 0);
}
我的清单文件是
<receiver android:name=".MainWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
每次单击按钮时,我都会得到放置在onReceive第一行的日志。您是说单击按钮时可以刷新小部件,这意味着getServerData(上下文)正在工作,那么您所说的“我无法调用名为getServerData()的方法”是什么意思?@vipul,它只在第一次加载小部件时调用,而不是单击按钮,然后“请浏览此链接我认为AppWidget中没有onclick事件。请显示onRecive方法好吗?”?
public void onReceive(Context context, Intent intent) {
Log.i("onreceive","called");
this.context=context;
AppWidgetManager manager = AppWidgetManager.getInstance(context);
// QuotesApp appState = ((QuotesApp) context.getApplicationContext());
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER))
{
}
// server data processing here.
mponentName thisWidget = new ComponentName(context, MainWidget.class);
remoteViews.setTextViewText(R.id.widget_project,project_hrs);
remoteViews.setTextViewText(R.id.widget_activity,activity_time);
manager.updateAppWidget(thisWidget, remoteViews);
super.onReceive(context, intent);
}