Android小部件:按钮在小部件中单击时调用方法

时间:2013-05-02 06:10:48

标签: android android-widget

由于我是Android小部件的新手,因此我无法调用我的方法,该方法用于从我的Appwidget执行来自Web服务的数据收集。这是我的代码是,

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

和buttonPressed(Context)是我的方法。定义为,

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>

请注意:我可以在点击按钮时刷新我的小部件,但我无法调用名为buttonPressed()的方法

请帮我解决这个问题。

提前致谢。

onreceive方法是,

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

每次点击按钮时,我都会获得放在onReceive第一行的日志。

0 个答案:

没有答案