Button on Widget Android

I've been trying to set the button on my homescreen widget to re-open the ConfigActivity. However I wasn't able to do it.

I've tried looking around on stackoverflow, android dev guide as well as other unofficial tutorials and have been trying all the suggested way but so far no luck.

here's the onUpdate() in AppWidgetProvider()

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent intent = new Intent(context, ConfigActivity.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

views.setOnClickPendingIntent(R.id.widget_refresh, pendingIntent);

I've tried putting Extra, Data, Flags, no luck.

ConfigActivity is just the Activity first launched when putting the widget on homescreen. R.layout.widget is the layout for the widget R.id.widget_refresh is the button id

is there anything else I have to do? Anything I'm missing?

my ConfigActivity.java

public class ConfigActivity extends Activity implements android.view.View.OnClickListener {

private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.configactivity);
    Intent intent = new Intent();
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    setResult(Activity.RESULT_CANCELED, intent);

    assignAppWidgetId();
    findViewById(R.id.widgetactivity_button).setOnClickListener(this);
}

/**
 * Widget configuration activity,always receives appwidget Id appWidget Id =
 * unique id that identifies your widget analogy : same as setting view id
 * via @+id/viewname on layout but appwidget id is assigned by the system
 * itself
 */
private void assignAppWidgetId() {
    Bundle extras = getIntent().getExtras();
    if (extras != null)
        appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
}


public void onClick(View v) {
    if (v.getId() == R.id.widgetactivity_button)
        startWidget();
}

/**
 * This method right now displays the widget and starts a Service to fetch
 * remote data from Server
 */
private void startWidget() {

    // this intent is essential to show the widget
    // if this intent is not included,you can't show
    // widget on homescreen
    Intent intent = new Intent();
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    setResult(Activity.RESULT_OK, intent);

    // start your service
    // to fetch data from web
    Intent serviceIntent = new Intent(this, RemoteFetchService.class);
    serviceIntent
            .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    startService(serviceIntent);

    // finish this activity
    this.finish();
}
}

EDIT: I've solved the problems, by putting the intents on the onReceive() instead of onUpdate(), I can get the widget to receive clicks like normal again.

链接地址: http://www.djcxy.com/p/50660.html

上一篇: 如何防止下载通知在Android API 10中消失

下一篇: 在Widget Android上的按钮