2012-12-22 41 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); 
       } 

      } 
     }