更新的Andr​​oid窗口小部件从活动 [英] Update Android Widget From Activity

查看:120
本文介绍了更新的Andr​​oid窗口小部件从活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小部件,其设置这样,当我点击它,它在活动中打开某些设置。

I have a widget, its setup so that when I click on it, it opens some settings in an activity.

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

该配置应用程序的某些设置。我想实现的是有小部件更新其视图以反映更改的设置时,活动一启动关闭。使用更新间隔或者任何其它类型的轮询是不适用于此。

This configures some settings for the application. What I want to achieve is to have the widget update its view to reflect the changed settings when the Activity I launch closes. Using the update interval or any other type of polling isn't appropriate for this.

我见过一对夫妇的地方在这里,并在Android文档此code使用的:

I've seen a couple places here and in the android docs this code used:

appWidgetManager.updateAppWidget(mAppWidgetId, views);

但我不知道怎么弄的mAppWidgetId值。我想在这里下面的示例中的小部件配置活动<一href="http://developer.android.com/guide/topics/appwidgets/index.html">http://developer.android.com/guide/topics/appwidgets/index.html,但在以下code,

But I don't know how to get the mAppWidgetId value. I tried following the example for a widget configuration activity here http://developer.android.com/guide/topics/appwidgets/index.html, but in the following code,

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

演员永远是空的,所以我从来没有得到AppWidgetID。

extras is always null, so I never get the AppWidgetID.

好了,现在我只是散漫。你觉得我还能做什么?

Ok, now I'm just rambling. What do you think I can do?

推荐答案

我终于找到了我一直在寻找的答案,那是在updateAppWidget函数的重载。

I finally found the answer I was looking for, it was in an overload of the updateAppWidget function.

   appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(), Widget.class.getName()), views);

这让我进入小部件,而无需知道appWidgetID。我最后的code在我的活动则:

This let me access the widget without having to know the appWidgetID. My final code in my activity is then:

        // Create an Intent to launch ExampleActivity
    Intent intent = new Intent(this, Settings.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

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

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
    appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(), Widget.class.getName()), views);
    finish();

我得做所有相同的设置的东西,我不得不这样做,在小部件的方法的OnUpdate,但现在我每次离开我活动了Widget是显示正确的状态。

I have to do all the same setup stuff I had to do in the onUpdate method of the Widget, but now every time I exit my activity the Widget is displaying the correct state.

这篇关于更新的Andr​​oid窗口小部件从活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆