1

因为我是 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 第一行的日志。

4

0 回答 0