2012-12-22 47 views
3

我遇到了一些問題,在我一直在處理的自定義啓動器中將小部件添加到主屏幕。自定義啓動器中的綁定小部件

我已經能夠通過AppWidgetManager生成一個要添加的小部件列表,並且我已經開發了將小部件添加到我的主屏幕的工作流程。該代碼是不太下面是什麼,但看起來像下面這樣:

AppWidgetHost widget_host = new AppWidgetHost(this, 1); 
AppWidgetManager widget_manager = AppWidgetManager.getInstance(this); 

int widget_id = widget_host.allocateAppWidgetId(); 
AppWidgetProviderInfo widget_provider = ... //from an array; 

Intent bindIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND); 
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id); 
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, widget_provider.provider); 
startActivityForResult(bindIntent, REQUEST_BIND_APPWIDGET); 

if (widget_provider.configure != null) { 
    Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); 
    intent.setComponent(widget_provider.configure); 
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id); 
    startActivityForResult(intent, REQUEST_CREATE_APPWIDGET); 
} else { 
    createWidget(widget_id); 
} 

我再有,如果有必要,導致部件的構造的onActivityResult方法,以及createWidget方法使用AppWidgetHost的CreateView的方法。

此工作流程工作正常,但ACTION_APPWIDGET_BIND意圖詢問用戶綁定應用程序的權限,這有點煩人。我的理解是,只有系統應用程序可以請求此權限,並且在綁定窗口小部件時我沒有運氣,而無需在應用程序運行時請求此權限。另一方面,我知道那裏有很多其他的發射器,他們都可以無縫地添加小部件,所以必須有另一種方法。

任何意見將不勝感激!

乾杯

+0

你有沒有找到解決你的問題? –

回答

3

希望,這個問題仍然是開放的......

你做你的方法中太多的東西。在特定情況下,您可以依次開啓簡短的事件。我在android上工作的時間不長,所以我不能告訴你,這是否可以。

而你總是在這裏火的意圖:

Intent bindIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND); 
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id); 
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, widget_provider.provider); 
startActivityForResult(bindIntent, REQUEST_BIND_APPWIDGET); 

意圖造成以上問題的最probarbly。您可以事先檢查您是否需要申請許可。你可以問這個例程:

Boolean callProviderIntent = false; 
if (checkCallProviderIntent) 
{ 
    callProviderIntent = true; 
    Method m = null; 
    try 
    { 
     m = AppWidgetManager.class 
      .getMethod("bindAppWidgetIdIfAllowed", new Class[] 
      { Integer.TYPE, ComponentName.class }); 
    } 
    catch (NoSuchMethodException e) 
    { 
    } 
    if (m != null) 
    { 
     try 
     { 
      callProviderIntent = !(Boolean) m 
      .invoke(mAppWidgetManager, 
        appWidgetId, 
        launcherAppWidgetInfo.provider); 
     } 
     catch (Exception e) 
     { 
     } 
    } 
} 

它是虛擬代碼。它使用反射,因爲我在Android 2.3下。

2

這是我終於在我的應用程序中找到的解決方案。

  AppWidgetManager manager = m.getAppWidgetManager(); 
      AppWidgetHost host = m.getWidgetHost(); 

      List<AppWidgetProviderInfo> widgetList = manager.getInstalledProviders(); 

      AppWidgetProviderInfo provider = null; 
      for(AppWidgetProviderInfo info : widgetList){ 
       //To get the google search box 
       if(info.provider.getClassName().equals("com.google.android.googlequicksearchbox.SearchWidgetProvider")){ 
        provider = info; 
        break; 
       } 
      } 
      if(provider != null){ 
       int id = host.allocateAppWidgetId(); 

       boolean success = false; 
       success = manager.bindAppWidgetIdIfAllowed(id, provider.provider); 
       if (success) { 
        AppWidgetHostView hostView = host.createView(getActivity(), id, provider); 
        AppWidgetProviderInfo appWidgetInfo = manager.getAppWidgetInfo(id); 

        LauncherAppWidgetInfo info = new LauncherAppWidgetInfo(id); 
        info.hostView = hostView; 
        info.hostView.setAppWidget(id, appWidgetInfo); 
        attachWidget(info); 
       } else { 
        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND); 
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id); 
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, 
          provider.provider); 
        // TODO: we need to make sure that this accounts for the options 
        // bundle. 
        // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, 
        // options); 
        m.startActivityForResult(intent, Main.REQUEST_BIND_APPWIDGET); 
       } 

      } 
     }