在RemoteViews中更新ImageView的正确方法?

时间:2014-10-23 07:07:38

标签: android android-appwidget remoteview

我尝试从远程URL获取图像并将其放入ImageView中。 ImageView本身位于app小部件中,因此从我目前的理解来看,这意味着它封装在RemoteViews中并且不能直接处理。

但我永远无法让app小部件显示图像。我使用AsyncTask扩展从远程URL获取图像作为位图,似乎正在获取图像OK(无论如何都返回非空位图),尽管我还不能显示它以确认。

我认为我必须使用错误的方法来实际更新app小部件的ImageView。

以下是所有相关代码:

布局/ new_app_widget.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/content" />
</RelativeLayout>

来自NewAppWidget.java的相关位:

public class NewAppWidget extends AppWidgetProvider {

...

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
        int appWidgetId) {

    // Construct the RemoteViews object
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

    String strURL = "http://example.com/YqLOSfr4.png";

    new ImageFetchAsyncTask(strURL, R.id.imageView).execute();
    // ^^^^^
    // in the above, I pass the URL and the id of the imageview, fetch
    // the bitmap, and in the onPostExecute() method I use
    // setImageViewBitmap(viewId, bitmap) to load the bitmap into the imageview
    // 
    // I don't have the code to hand, but I can provide it.  For now, someone
    // may be able to tell me somewhere else I'm doing something wrong

    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

}

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mike.widgetapptest" >
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        android:debuggable="true"
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".NewAppWidget" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

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

</manifest>

1 个答案:

答案 0 :(得分:2)

Classic beginner的错误......我试图在异步任务之后立即执行某些操作,期望异步任务完成。所以我将依赖于异步任务结果的内容移动到异步任务的onPostExecute()方法中,并且在我的应用小部件中显示了图像!