限制的Andr​​oid窗口小部件,以每个设备一个实例部件、实例、窗口、设备

2023-09-05 00:23:02 作者:爱一瞬间的悲伤

我怎么能限制只有一个实例可以由用户在任何时候都可以创建一个方法的Andr​​oid窗口小部件?

一个可能的方式是存储共享preference 包括柜台变量和崩溃,如果数为1,但显然我不赞成这种解决方案。 ; - )

解决方案   

我怎么能限制只有一个实例可以由用户在任何时候都可以创建一个方法的Andr​​oid窗口小部件?

您不能。

然而,仅仅因为用户要求您的应用程序窗口小部件的多个实例并不意味着你必须要管理每个单独的数据。只是忽略的ID,并使用 updateAppWidget()方法,该方法不带任何标识。

How could I restrict Android widgets in a way that only one instance can be created by the user at all times?

A possible way is to store a SharedPreference including a counter variable and crash, if the count is 1, but obviously I'm not in favor of that solution. ;-)

解决方案

How could I restrict Android widgets in a way that only one instance can be created by the user at all times?

You can't.

However, just because the user asks for multiple instances of your app widget does not mean you have to manage separate data for each. Just ignore the IDs and use the updateAppWidget() method that does not take any IDs.