如何在Widget配置活动中获取小部件名称(提供者)?

时间:2018-03-28 15:00:31

标签: android android-widget

我有Small和Big小部件,两者都是通过相同的WidgetConfigActivity配置的。摘自AndroidManifest:

    <receiver
        android:name=".SmallWidgetProvider"
        android:icon="@drawable/small_widget_icon"
        android:label="@string/small_widget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_small_info" />
    </receiver>

    <receiver
        android:name=".BigWidgetProvider"
        android:icon="@drawable/big_widget_icon"
        android:label="@string/big_widget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_big_info" />
    </receiver>

widget_small_info.xml:

<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget_small_layout"
    android:minHeight="40dp"
    android:minWidth="40dp"
    android:previewImage="@drawable/widget_small_preview"
    android:updatePeriodMillis="1200000"
    android:configure="com.a.b.WidgetConfigActivity">

</appwidget-provider>

我知道如何获取小部件的ID,但这并不能告诉我它是什么类型的小部件:

    Bundle extras = intent.getExtras();
    if (extras != null) {
        mAppWidgetId = extras.getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }

如何区分WidgetConfigActivity中的Small和Big小部件?我想我应该检查provider(Doc:这个字段对应于AndroidManifest.xml文件中元素中的android:name属性。

AppWidgetManager.EXTRA_APPWIDGET_PROVIDER

但这不是附加内容。

2 个答案:

答案 0 :(得分:1)

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
AppWidgetProviderInfo providerInfo = appWidgetManager.getAppWidgetInfo(mAppWidgetId);

this answer找到了这个。

答案 1 :(得分:0)

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); AppWidgetProviderInfo appWidgetManager.getAppWidgetInfo(widgetid);