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