3

我正在尝试为设备开发一个 android 应用程序(设备不是平板电脑或手机,它是运行 Android 4.1.1 的预配置 android 板),其中一个活动需要显示几种类型信息的自定义仪表板,如下所示 在此处输入图像描述

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

我参考了以下链接

将小部件添加到我自己的应用程序中。- 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 is null” 它没有找到要添加到我的视图的小部件

请帮忙

4

2 回答 2

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");
        }
于 2012-11-20T21:50:37.917 回答
1

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

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

于 2012-11-20T20:37:37.473 回答