2

我想为我的 android 应用程序创建一个小部件,显示一些关于位置的实时详细信息,例如天气。但我想在任何时间点允许最多 3 个小部件实例,每个实例都有不同的位置。我不知道该怎么做,也找不到任何信息。

我的问题是:

  1. 可以创建的 appWidget 实例的数量是否有限制?
  2. 如何限制用户在任何时候可以创建的小部件实例的数量?
4

1 回答 1

0

there are multiple ways to count the app widgets that i can think of:

  1. send an intent to all of your app widgets to see that they still exist, and count them, and only in the end you would know how many there are. however, i'm not sure how you would know when to stop counting.

  2. not sure if it works but maybe you could use:    

    final ComponentName cn=new ComponentName(context,YourAppWidgetProviderClass.class);
    int[] appWidgetsIds=appWidgetManager.getAppWidgetIds(cn);
    

    and check the size of the result.

  3. update the counter based on receving the ACTION_APPWIDGET_DELETED (or ACTION_APPWIDGET_DISABLED) and ACTION_APPWIDGET_BIND (or ACTION_APPWIDGET_ENABLED) actions in the intents on the onReceive() method of your AppWidgetProvider class.

anyway, if there are too many (4 or more in your case) , just show an alternative layout for the new widget.

you would also need to handle the case of removing app widgets (those that weren't disabled) so that you would enable new ones (and decrease the counter), or you could let the user click on disabled ones to trigger the check again.

about limiting the creation itself, i don't think it's possible, but as i wrote, it's possible to count them and disable new ones.

do note that the user might have the app widget available on more than one app . it doesn't even have to be a launcher that will show your app widget. also not sure how it would work in case there are multiple users (available on android 4.2 and above) .

于 2013-08-26T17:48:00.607 回答