0

我有自动添加到启动器的小部件,但其中一些小部件需要配置,所以我设置了一个 onClick 侦听器来获取 id 并获取小部件信息以启动 Activity 以配置小部件,但我遇到的问题是当我从天气小部件的 Activity 返回时,它失败的部分在 create 方法中,当我尝试通过小部件 id 获取 appWidgetInfo 时,但 id 带着意图返回。我不知道出了什么问题,有人可以帮我解决这个问题吗?

    AppWidgetManager mAppWidgetManager;
    AppWidgetHost mAppWidgetHost;
    LinearLayout mainlayout;
    ComponentName cn = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mainlayout = (LinearLayout) findViewById(R.id.mylauncher);
    mainlayout.setOnClickListener(this);

    mAppWidgetManager = AppWidgetManager.getInstance(this);
    mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID);

    // search widgets
    searchWidgets();

}





public void searchWidgets() {
    String widget = "com.weather.Weather.widgets.WeatherWidgetProvider4x1";
    try {

        mAppWidgetManager = AppWidgetManager.getInstance(getBaseContext());
        final List<AppWidgetProviderInfo> infos = mAppWidgetManager
                .getInstalledProviders();
        AppWidgetProviderInfo appWidgetInfo = null;
        for (final AppWidgetProviderInfo info : infos) {
            Log.v("Available Widgets", "label " + info.label + " ::"
                    + info.provider.getPackageName() + " / "
                    + info.provider.getClassName());
        }

        for (final AppWidgetProviderInfo info : infos) {
            Log.v("Searching for Widget class",
                    info.provider.getPackageName() + " / "
                            + info.provider.getClassName());

            if (widget.equals(info.provider.getClassName())) {
                String packageName = info.provider.getPackageName()
                        .toString();
                cn = new ComponentName(packageName, widget);
                // found it
                appWidgetInfo = info;
                Log.d("FOUND !!!!!!!!!", "Widget class " + packageName);

                break;
            }

        }

        mAppWidgetHost = new AppWidgetHost(getBaseContext(),
                R.id.APPWIDGET_HOST_ID);

        if (appWidgetInfo == null) {
            Log.d(TAG, cn + " No such widget available returning view");

            // return; // stop here
        } else {

            int appWidgetId = mAppWidgetHost.allocateAppWidgetId();

            AppWidgetHostView hostView = mAppWidgetHost.createView(this,
                    appWidgetId, appWidgetInfo);
            hostView.setAppWidget(appWidgetId, appWidgetInfo);

            AppWidgetHostView hostViewsecond = mAppWidgetHost.createView(
                    this, appWidgetId, appWidgetInfo);
            hostView.setAppWidget(appWidgetId, appWidgetInfo);

            hostView.setId(appWidgetInfo.previewImage);

            Drawable draw = null;
            String packageName = appWidgetInfo.provider.getPackageName();
            try {
                draw = this.getPackageManager().getDrawable(packageName,
                        appWidgetInfo.previewImage,
                        this.getPackageManager().getApplicationInfo(packageName,
                                0));
            } catch (NameNotFoundException e) {
                Log.d(TAG, "drawable not found",e);
            }

            ImageView preViewWigdet = new ImageView(this);
            preViewWigdet.setImageDrawable(draw);
            preViewWigdet.setId(appWidgetInfo.previewImage);


            mainlayout.addView(preViewWigdet);

        }

    } catch (SecurityException securityEx) {
        Log.e("Bind Widget",
                "Error device not rooted,  returning view, Security exception:- ",
                securityEx);

    }
}



@Override
public void onClick(View v) {
    AppWidgetProviderInfo widgetItem = null;
    mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID);
    int appWidgetId = mAppWidgetHost.allocateAppWidgetId();

    if (view.getClass().equals(ImageView.class)){
    // putting installed Providers in a list
    final List<AppWidgetProviderInfo> infos = mAppWidgetManager
            .getInstalledProviders();
    for (int i = 0; i < infos.size(); i++) {
        if (infos.get(i).previewImage == view.getId()) {
            widgetItem = (AppWidgetProviderInfo) infos.get(i);
        }
    }
    ComponentName cn = new ComponentName(
            widgetItem.provider.getPackageName(),
            widgetItem.provider.getClassName());

    int[] ids;
    ids = mAppWidgetManager.getAppWidgetIds(cn);
    Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);

    intent.setComponent(widgetItem.configure);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

    if (widgetItem.configure != null) {
        Log.d(TAG, "widget to be configured " + widgetItem.label);
        ((Activity) this).startActivityForResult(intent,
                R.id.REQUEST_CREATE_APPWIDGET);
    }

}
}



/**
 * If the user has selected an widget, the result will be in the 'data' when
 * this function is called.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "" + resultCode);
    if (resultCode == RESULT_OK) {
        if (requestCode == R.id.REQUEST_PICK_APPWIDGET) {
            configureWidget(data);
        }
        if (requestCode == R.id.REQUEST_CREATE_APPWIDGET) {
            mAppWidgetManager = AppWidgetManager.getInstance(this);
            createWidget(data);
        }
    } else if (resultCode == RESULT_CANCELED && data != null) {
        int appWidgetId = data.getIntExtra(
                AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
        if (appWidgetId != -1) {
            mAppWidgetHost.deleteAppWidgetId(appWidgetId);
        }
    }
}

/**
 * Checks if the widget needs any configuration. If it needs, launches the
 * configuration activity.
 */
private void configureWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras
            .getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager
            .getAppWidgetInfo(appWidgetId);
    if (appWidgetInfo.configure != null) {
        Intent intent = new Intent(
                AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
        intent.setComponent(appWidgetInfo.configure);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        startActivityForResult(intent, R.id.REQUEST_CREATE_APPWIDGET);
    } else {
        createWidget(data);
    }
}

/**
 * Creates the widget and adds to our view layout.
 */
public void createWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras
            .getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);

  //this is where I am having the problem, appWidgetInfo returns null
    AppWidgetProviderInfo appWidgetInfo = this.mAppWidgetManager
            .getAppWidgetInfo(appWidgetId);

    AppWidgetHostView hostView = mAppWidgetHost.createView(this,
            appWidgetId, appWidgetInfo);
    hostView.setAppWidget(appWidgetId, appWidgetInfo);
    mainlayout.addView(hostView);

    Log.i(TAG, "The widget size is: " + appWidgetInfo.minWidth + "*"
            + appWidgetInfo.minHeight);
}
4

1 回答 1

0

我猜“this.mAppWidgetManager.getAppWidgetInfo(appWidgetId)”行返回 null,因为我无法在代码中将 Appwidget id 绑定到 AppwidgethostInfo 提供程序的任何地方看到

尝试用谷歌搜索 bindAppWidgetIdIfAllowed 函数以查看它是如何工作的,您可能需要对 ACTION_APPWIDGET_BIND 进行另一个 Activity 调用

于 2014-02-13T22:49:16.557 回答