I have a widget for my app. On widget Creation when android triggers onEnabled
I'm checking if a user meets a certain requirement then the user can go ahead and create a widget. But I need to stop widget creation if they don't meet a certain requirement. I can't figure out how to cancel widget creation dynamically. Here's what I was trying which didn't work.
RemoteViews views;
void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
views = new RemoteViews(context.getPackageName(), R.layout.lock_widget);
views.setOnClickPendingIntent(R.id.lock_widget, getPendingSelfIntent(context, LOCK));
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
// Query User pro status when creating widget
Log.d("Widget", "OnEnabled Fired");
views.removeAllViews(views.getLayoutId());
//views.
}
views.removeAllViews(views.getLayoutId());
doesn't seem to work.
Is there even a way to this. The workaround that I'm using is checking certain requirement check in onUpdate
.
答案 0 :(得分:0)
如果您具有所有“某些要求”,那么请启用您的案例中的最佳解决方案。
所以我假设以下情况。用户使用您的应用程序,并且在某些情况下(启用窗口小部件组件,如果用户失去了“某些要求”,则仅禁用窗口小部件组件,因此窗口小部件选择器中没有窗口小部件)。您可以通过PackageManager轻松实现此目标。
已更新:当用户具有专业状态时,只需启用小部件即可,否则请禁用
public static void setComponentState(Context context, boolean enabled) {
int flag = (enabled ?
PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
ComponentName component = new ComponentName(context, YourAppWidgetProvider.class); // or make component via package name and class name (check Component constructors)
PackageManager pm = context.getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(component, flag, PackageManager.DONT_KILL_APP);
}