【问题标题】:How to update the widget from the widgetConfigure class如何从 widgetConfigure 类更新小部件
【发布时间】:2014-10-12 23:35:39
【问题描述】:

我有一个WidgetConfigure 类,它在将小部件放在主屏幕上后被调用。但是如何在完成配置活动后开始更新我的小部件?我想在 WidgetProvider 类中调用 onUpdate 方法,因为我的 Http 请求在那里完成。

配置类:

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

            }
        });
    }

}

我不想使用 android 文档中的示例代码,因为这里没有来自 Http 请求的数据:

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

那么有什么方法可以在我的 WidgetProvider 类中执行onUpdate() 吗?

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    您可以通过向 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);
    }
    

    在(例如)Activity 中调用方法,但您可以在任何其他地方执行此操作。

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

    在这个 onUpdate 方法之后,所有 id 作为第二个参数传递的小部件都会被调用。

    您还必须在 AndroidManifest.xml 中将操作 AppWidgetManager.ACTION_APPWIDGET_UPDATE 添加到您的小部件:

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

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 2020-01-13
      • 1970-01-01
      • 2011-05-03
      • 2021-07-28
      相关资源
      最近更新 更多