我可以将安装的应用程序中的小部件添加到我的应用程序活动中

时间:2012-11-20 20:25:04

标签: android android-widget

我正在尝试为设备开发Android应用程序(设备不是平板电脑或手机,它是运行Android 4.1.1的预配置安卓板),其中一项活动需要显示几种类型信息的自定义仪表板如下所示 enter image description here

此处显示的信息类型已预先配置。我没有从头开始开发所有这些,而是​​计划安装具有小部件的应用程序(如天气和日历),并在我的布局中包含小部件。

我参考了以下链接

Add widgets into my own application.- Android

http://www.anddev.org/appwidgethost_tutorial-t10329.html

我完成了大部分代码。

public class MainActivity extends Activity {


    private AppWidgetManager mAppWidgetManager;
    private AppWidgetHost mAppWidgetHost;
    static final int APPWIDGET_HOST_ID = 2037;
    private AppWidgetHostView widgets[] = new AppWidgetHostView[16];



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAppWidgetManager = AppWidgetManager.getInstance(this);
        mAppWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);

        setContentView(r.id.layout_main);


    ****here is where I am stuck??****
        int [] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(name);


        mAppWidgetHost.startListening();

    }

但是我被困的地方是,如何指定小部件管理器来获取我想要的特定小部件。让我们说我对小部件感兴趣 的 “com.android.deskclock / com.android.alarmclock.AnalogAppWidgetProvider”

请建议。


更新 我添加了以下代码

 mAppWidgetManager = AppWidgetManager.getInstance(this);
        mAppWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);
        int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
        ComponentName name = new ComponentName("com.android.deskclock","com.android.alarmclock.AnalogAppWidgetProvider");

        int [] ids = mAppWidgetManager.getAppWidgetIds(name);

        if(ids != null && ids.length>0){
            System.err.println("Number of widget ids :"+ids.length);
            AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(ids[0]);
            appWidgetId = ids[0];
            LinearLayout parent= (LinearLayout) findViewById(R.id.generalDashboardLayout);
            parent.addView(mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo));
        }else{
            System.err.println("Ids is null");
        }

它总是打印“Ids为null”它没有找到要添加到我的视图的小部件

请帮忙

2 个答案:

答案 0 :(得分:2)

最后,我使用下面的代码解决了这个问题

mAppWidgetManager = AppWidgetManager.getInstance(this);
        mAppWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);
        int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
        ComponentName name = new ComponentName("com.android.deskclock","com.android.alarmclock.AnalogAppWidgetProvider");

        int [] ids = mAppWidgetManager.getAppWidgetIds(name);

        if(ids != null && ids.length>0){
            System.err.println("Number of widget ids :"+ids.length);
            AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(ids[0]);
            appWidgetId = ids[0];
            LinearLayout parent= (LinearLayout) findViewById(R.id.generalDashboardLayout);
            parent.addView(mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo));
        }else{
            System.err.println("Ids is null");
        }

答案 1 :(得分:1)

是的,你可以。您需要使用AppWidgetHost类,就像所有主屏幕应用程序一样。

这是一个源代码主屏幕应用程序,您可以使用它来学习如何使用它。 http://code.google.com/p/android-launcher-plus/