8

我正在尝试在 onUpdate 的小部件布局中的图像视图中设置图像,但图像没有更新

 @Override
     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
     {
         Log.i(TAG, "onUpdate called");
         for (int appWidgetId : appWidgetIds) 
         {
             Bitmap bitmap=ImageUtils.getBitmap(context, appWidgetId);
             RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);

             remoteView.setImageViewBitmap(R.id.imgView,bitmap);
             appWidgetManager.updateAppWidget(appWidgetId, remoteView);
         }
     }

小部件的xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

清单文件

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
             android:name=".WidgetConfig"
             android:label="@string/app_name" >
             <intent-filter>
                  <action
                    android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
             </intent-filter>
         </activity>
        <receiver
            android:name="BasicWidgetProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>

          <receiver
            android:name="BasicWidgetProvider">
            <intent-filter>
                <action
                    android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                    <data android:scheme="widget" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>
    </application>

小部件信息.xml

<appwidget-provider 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="25dp"
    android:minHeight="25dp"
    android:initialLayout="@layout/widget"
    android:configure="com.demo.WidgetConfig" 
    android:widgetCategory="home_screen|keyguard" 
    android:previewImage="@layout/widget" 
    android:resizeMode="horizontal|vertical" 
    android:initialKeyguardLayout="@layout/widget" 
    android:minResizeHeight="5dp" 
    android:minResizeWidth="5dp">
</appwidget-provider>

但是当尝试加载小部件时,我得到一个 Toast “应用程序未安装”,我没有显示任何这样的 toast 来自哪里?

为什么图像没有设置,如何解决?

4

3 回答 3

1

确保在 appwidget-provider xml 的“android:configure”字段中设置了正确的包名称,并且在清单文件中设置了正确的 intetn-filter。

于 2013-05-07T11:21:42.623 回答
0

请尝试如下所示的 remoteview.setimageViewuri。

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        thisWidget = new ComponentName(context, HelloWidget.class);
        remoteViews.setImageViewUri(R.id.imageView1,Uri.parse("/mnt/sdcard/test.png")); 
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    }
于 2013-05-07T06:42:39.430 回答
0
<receiver
    android:name="BasicWidgetProvider">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/widget_info" />
</receiver>

您在姓名前缺少点,并且您有两次此接收器。

于 2013-05-07T21:30:21.710 回答