简体   繁体   English

如何在应用启动时更新小部件

[英]How to update a widget on app start

I have a widget working with my app. 我有一个与我的应用程序一起工作的小部件。 When the app is first started I keep the context, and values so I can call my UpdateWidget() from the app when things change. 首次启动应用程序时,我会保留上下文和值,以便在事情发生变化时可以从应用程序中调用UpdateWidget()。 But if the app doesnt know what the context and AppID is I dont know how to get that information. 但是,如果应用程序不知道上下文和AppID是什么,我将不知道如何获取该信息。 I think the way I tried to do this is wrong anyway, but not sure what else to try. 我认为我尝试执行此操作的方法无论如何都是错误的,但不确定是否还有其他尝试。

So basically the widget is already on the desktop. 因此,基本上该小部件已经在桌面上了。 I start up the app and make some changes and I need to send an update to the app with the new info. 我启动了应用程序并进行了一些更改,我需要使用新信息向应用程序发送更新。

I would have thought the app would get this connection to the widget when it starts up, but not the case. 我本来以为应用程序启动时会与小部件建立这种连接,但事实并非如此。

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();

        Ct = context;
        ApMgr = appWidgetManager;
        WidgetID = appWidgetIds;

        WidgetOn = true;

        UpdateWidget();

    }

When the app is first started I keep the context 首次启动应用程序时,我会保留上下文

Unless that is the application context, you are leaking memory. 除非那是应用程序上下文,否则您正在泄漏内存。 Please do not leak memory. 请不要泄漏内存。

and values so I can call my UpdateWidget() from the app when things change 和值,以便在事情发生变化时可以从应用程序中调用我的UpdateWidget()

Any place in your app where you could possibly need to update the widget, you already have a Context . 应用程序中可能需要更新小部件的任何位置,都已经有一个Context For example: 例如:

But if the app doesnt know what the context and AppID is I dont know how to get that information. 但是,如果应用程序不知道上下文和AppID是什么,我将不知道如何获取该信息。

You absolutely know "what the context" is. 您绝对知道“上下文”是什么。 It is your Activity . 这是你的Activity Or, if the code in question is in your custom Application object, "the context" is the Application . 或者,如果所讨论的代码在您的自定义Application对象中,则“上下文”为Application

With respect to the app widget ID, store it in a file. 关于应用程序小部件ID,将其存储在文件中。 Or a database. 或数据库。 Just be sure to clean it up when the app widget is uninstalled. 只需确保在卸载应用程序小部件时将其清理即可。

Or, if you are not supporting multiple app widgets, just skip the app widget ID altogether, and use the updateAppWidget() method that does not take an array of app widget IDs. 或者,如果您不支持多个应用程序小部件,只需完全跳过应用程序小部件ID,然后使用不带应用程序小部件ID数组的updateAppWidget()方法。

I figured this out with the great help and clues from CommonsWare. 我从CommonsWare的强大帮助和线索中弄清楚了这一点。 What I was trying to do is make sure the app could send an update to the widget when something changes on the app database. 我要尝试做的是确保当应用程序数据库上发生某些更改时,该应用程序可以将更新发送给小部件。 there is no need to update the widget any other time. 无需其他时间更新小部件。 When the app gets disconnected from the widget from a reboot or new version the link was severed. 当应用因重新启动或新版本而与小部件断开连接时,链接已断开。 this little function below gets the "link" back again. 下面的这个小功能再次使“链接”恢复。 Thanks again to Stackoverflow and CommonsWare!! 再次感谢Stackoverflow和CommonsWare! I hope I did this correctly, it does work the want I wanted it to. 我希望我正确地做到了这一点,并且可以按我想要的方式工作。

public static void UpdateWidget(Context Ct)
{

        ComponentName Me =new ComponentName(Ct, Widget.class);

        remoteViews=new RemoteViews(Ct.getPackageName(), R.layout.widget);

        AppWidgetManager ApMgr=AppWidgetManager.getInstance(Ct);

        DisplayTop(Ct); // This gets the data from the DB and updates the remote view

        ApMgr.updateAppWidget( Me, remoteViews);

}

It works completely and without trouble.You can only use; 它完全可以正常工作,只能使用。

int[] ids = AppWidgetManager.getInstance(Context.getApplication()).getAppWidgetIds(new ComponentName(Context.getApplication(), Widget.class));
                    Widget myWidget = new Widget();
                    myWidget.onUpdate(Context, AppWidgetManager.getInstance(Context),ids);

I do this for my AppWidget without passing the AppWidget ID and it works fine. 我在不传递AppWidget ID的情况下为我的AppWidget执行了此操作,并且工作正常。

Context context = getApplicationContext();
context.startService(new Intent(context, UpdateService.class));

or if you know the context you can do something like this - 或者,如果您知道上下文,则可以执行以下操作-

startService(new Intent(myClass.this, UpdateService.class));

Both works!!! 两者都可以!!!

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

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