1
我已經構建了一個允許多個尺寸爲2x2和4x2的Android小部件。所以當有人在窗口部件選擇器屏幕上時,他們會看到兩種尺寸。他們點擊其中一個尺寸,然後進入我的配置活動。我如何知道他們在小工具選取器屏幕上挑選的尺寸。我需要知道在配置活動中選擇了哪一個。我正在遇到如何前進的問題以及要研究的方向。下面是我的清單文件,以防萬一你認爲它可以解釋我在做什麼:Android widget您確定他們在配置活動中選擇了多大的尺寸
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.corradodev.customizable_countdown_widget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<!-- Configure Activity -->
<activity android:name=".configure">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<!-- Broadcast Receivers that will process AppWidget updates -->
<receiver android:name=".customizable_countdown_widget_large" android:label="@string/app_name_large">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider_large" />
</receiver>
<receiver android:name=".customizable_countdown_widget_small" android:label="@string/app_name_small">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider_small" />
</receiver>
<!-- Service for updating -->
<service android:name=".widget_service" />
</application>
</manifest>
感謝您的幫助。
解決方案: 在配置活動的OnCreate
Bundle mExtras = mIntent.getExtras();
if (mExtras != null) {
mAppWidgetId = mExtras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
//Log.d(TAG, Integer.toString(mAppWidgetId));
// If they gave us an intent without the widget id, close
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
AppWidgetProviderInfo providerInfo = AppWidgetManager.getInstance(getBaseContext()).getAppWidgetInfo(mAppWidgetId);
String minWidth=String.valueOf(providerInfo.minWidth);
謝謝。我沒有意識到你可以從小部件提供者xml獲取數據。我將完整的代碼添加到了我的問題中。再次感謝。 – 2012-03-02 03:04:08
獲取AppWidgetProviderInfo後,您也可以使用提供者字段而不是檢查minSize。 – phaethon 2013-10-14 09:10:48