【问题标题】:Dynamically deleting Android Widget from home screen从主屏幕动态删除 Android Widget
【发布时间】:2019-11-06 08:19:11
【问题描述】:

在我从配置活动中按下后,我需要删除一个主屏幕小部件实例,因为我希望它被完全删除,而不是因为issue 2539 而留在“边缘”中。所以最好是以下修复之一:

  1. 修复问题 2539 并让小部件实例从主屏幕和“边缘”中优雅地消失
  2. 让程序员通过 AppWidgetHost 使用正确的 id 引用主屏幕来执行此操作,(证明此安全漏洞)。 (有趣的尝试描述了here

现在这些都有可能吗?

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    我自己解决了,只处理两个布尔标志。 这是我在扩展 AppWidgetProvider 的类上所做的

    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        SharedPreferences settings = context.getSharedPreferences(SHARED_PREFERENCES, 0); 
    
        for(int widgetId:appWidgetIds)
        {   
            boolean configured = settings.getBoolean(CONFIGURED_PREFERENCE+widgetId, false);    //In order to skip initial UpdateService
            boolean widget= settings.getBoolean(WIDGET_PREFERENCE+widgetId, false);
             if(!widget && configured) continue;   // In order to skip phantom Widgets update
    
    
        if(!configured)
        {   
    
         SharedPreferences.Editor editor = settings.edit();
         editor.putBoolean(CONFIGURED_PREFERENCE+widgetId, true);
               editor.commit();
            }
        else
        {    Intent updateService=new Intent(context, UpdateService.class);          
            updateService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,widgetId);
             context.startService(updateService);
        }   
    
    
      }
    
    }
    

    【讨论】:

    • 链接好像坏了
    猜你喜欢
    • 2022-12-05
    • 1970-01-01
    • 2011-11-28
    • 2023-04-09
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多