简体   繁体   English

如何从widgetConfigure类更新小部件

[英]How to update the widget from the widgetConfigure class

I have a WidgetConfigure class which gets called after placing the widget on the home screen. 我有一个WidgetConfigure类,将其放置在主屏幕上后会被调用。 But how can I start update my widget after finishing the configure activity? 但是,完成配置活动后如何开始更新窗口小部件? I want to call the onUpdate method inside my WidgetProvider class because my Http request is done there. 我想在我的WidgetProvider类中调用onUpdate方法,因为我的Http请求已在此处完成。

Configure class: 配置类:

public class ExampleAppWidgetConfigure extends Activity {
    private int mAppWidgetId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setResult(RESULT_CANCELED);

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID, 
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        //No valid ID, so bail out.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
            finish();

        // config
        Button button = (Button) findViewById(R.id.save);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                TextView vendorIDTextView = (TextView) findViewById(R.id.vendorID);
                TextView apiKeyTextView = (TextView) findViewById(R.id.apiKey);
                Spinner currencySpinner = (Spinner) findViewById(R.id.currencySpinner);

                String vendorID = vendorIDTextView.getText().toString();
                String apiKey = apiKeyTextView.getText().toString();
                String currency = currencySpinner.getSelectedItem().toString();

                if (vendorID.isEmpty() || apiKey.isEmpty() || currency.isEmpty()) {
                    Toast.makeText(getApplicationContext(), getString(R.string.emptyForm), Toast.LENGTH_LONG).show();
                }
                else {
                    // save new values in preferences
                    SharedPreferences settings = getApplicationContext().getSharedPreferences(ExampleAppWidgetProvider.PREFS_NAME, 0);
                    SharedPreferences.Editor editor = settings.edit();
                    Log.i("LOG", "vendorID: " + vendorID);
                    Log.i("LOG", "apiKey: " + apiKey);
                    editor.putString("vendorID", vendorID);
                    editor.putString("apiKey", apiKey);
                    editor.putString("currency", currency);
                    editor.commit();

                    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());

                    // Update the widget ?

                    Intent resultValue = new Intent();
                    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
                    setResult(RESULT_OK, resultValue);
                    finish();
                }

            }
        });
    }

}

I do not want to use the example code in the android docs, because I do not have my data from the Http request here: 我不想在android docs中使用示例代码,因为这里没有来自Http请求的数据:

RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
appWidgetManager.updateAppWidget(mAppWidgetId, views);

So is there any method to execute onUpdate() in my WidgetProvider class? 那么,在我的WidgetProvider类中是否有任何方法可以执行onUpdate()

You can call onUpdate method by sending broadcast to AppWidgetProvider: 您可以通过将广播发送到AppWidgetProvider来调用onUpdate方法:

void updateWidget(Context c,int[] widgets_ids,Class<?> widget_class)
{
    Intent update_intent = new Intent();
    update_intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    update_intent.setClass(c, widget_class);
    update_intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgets_ids);
    c.sendBroadcast(update_intent);
}

Invoke method in (for example) Activity, but you can do this in any other place. 在(例如)“活动”中调用方法,但是您可以在其他任何地方执行此操作。

updateWidget(this,new int[]{widget_id},YourAppWidgetProviderClass.class);

After this onUpdate method will be called for all widgets whose ids are passed as second parameter. 此后,将为所有其ID作为第二个参数传递的窗口小部件调用onUpdate方法。

You also must have action AppWidgetManager.ACTION_APPWIDGET_UPDATE added in AndroidManifest.xml to your widget: 您还必须在AndroidManifest.xml中将操作AppWidgetManager.ACTION_APPWIDGET_UPDATE添加到窗口小部件:

<receiver...
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
...
</intent-filter>
</receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM