How to update a widget on app start

2019-09-19 19:14发布

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. But if the app doesnt know what the context and AppID is I dont know how to get that information. 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();

    }

4条回答
太酷不给撩
2楼-- · 2019-09-19 19:45

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);
查看更多
Melony?
3楼-- · 2019-09-19 19:51

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

Any place in your app where you could possibly need to update the widget, you already have a Context. For example:

But if the app doesnt know what the context and AppID is I dont know how to get that information.

You absolutely know "what the context" is. It is your Activity. Or, if the code in question is in your custom Application object, "the context" is the Application.

With respect to the app widget ID, store it in a file. 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.

查看更多
冷血范
4楼-- · 2019-09-19 19:58

I figured this out with the great help and clues from 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!! 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);

}
查看更多
【Aperson】
5楼-- · 2019-09-19 20:10

I do this for my AppWidget without passing the AppWidget ID and it works fine.

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!!!

查看更多
登录 后发表回答