【问题标题】:Image in Widget小部件中的图像
【发布时间】:2013-04-26 22:26:27
【问题描述】:

我正在尝试在 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>

widget_info.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 “App isnt installed”,我没有显示任何这样的 toast 来自哪里??

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

【问题讨论】:

  • 加载时是否没有图像?对我来说,这听起来像是小部件/应用程序配置而不是图像的错误。您可以发布清单吗?
  • 我添加了清单文件,查看代码

标签: android android-widget android-appwidget remoteview appwidgetprovider


【解决方案1】:

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

【讨论】:

  • 他说的是widget provider xml而不是layout。
  • 好的,我发布了有问题的评论并在这里回复了同样的问题,没关系,我的错,再次更新
【解决方案2】:

请尝试使用下面的 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);
    }

【讨论】:

    【解决方案3】:
    <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>
    

    你的名字前面少了一个点,你有这个接收器两次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-29
      • 2018-09-05
      • 2022-08-15
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      相关资源
      最近更新 更多