Android:以编程方式启用/禁用应用小部件 [英] Android: enable/disable app widgets programmatically

查看:61
本文介绍了Android:以编程方式启用/禁用应用小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:有没有办法以编程方式启用我的应用程序提供的一些主屏幕小部件?例如,拥有一个高级"小部件并仅在付款后才能访问它?

Question: Is there a way to enable some of the homescreen widgets that I give out with my app programmatically? For example, having a "premium" widget and giving access to it only after payment?

正如 Android docs 所说,应该添加一个广播接收器在清单中告诉系统应用附带了一个小部件:

As the Android docs say, one should add a broadcast receiver in the manifest to tell the system that there is a widget coming with the app:

<receiver android:name="ExampleAppWidgetProvider" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
           android:resource="@xml/example_appwidget_info" />
</receiver>

但理论上广播接收器也可以通过编程方式添加,那么小部件可以在运行时稍后注册吗?

But in theory broadcast receivers can also be added programmatically, so can the widgets be registered later at runtime?

推荐答案

您可以在 元素上设置 android:enabled="false"清单中的应用小部件,然后使用 PackageManagersetComponentEnabledSetting() 在用户执行某些操作(例如付款)时在运行时启用它.

You can have android:enabled="false" on the <receiver> element for the app widget in the manifest, then use PackageManager and setComponentEnabledSetting() to enable it at runtime when the the user does something (e.g., pay up).

但是,这可能需要重新启动,然后主屏幕才会意识到存在新的潜在应用小部件提供程序.

However, it is possible that this will require a reboot before the home screen realizes that there is a new potential app widget provider.

这篇关于Android:以编程方式启用/禁用应用小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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