簡體   English   中英

從主屏幕動態刪除Android Widget

[英]Dynamically deleting Android Widget from home screen

從配置活動中按回后,我需要刪除主屏幕小部件實例,因為我希望它被完全刪除,而不是由於問題 2539而留在“邊緣”中。 所以這些修復之一會很好:

  1. 修復問題 2539 並讓小部件實例從主屏幕和“limbo”中優雅地消失
  2. 讓程序員通過 AppWidgetHost 執行此操作,並使用正確的 id 引用主屏幕,(使此安全漏洞證明)。 這里描述有趣的嘗試)

現在有可能嗎?

我自己解決了,只處理兩個布爾標志。 這是我在擴展 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);
    }   


  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM