android - AppWidget 刷新 RemoteViews 的 Uri

标签 android uri android-appwidget remoteview

我创建了一个 Appwidget,它显示通过 Uri 提供给 RemoteViews 的图像文件 (test.png)。 在 onUpdate 中,我运行一项更改文件内容的服务。我还为将调用 onUpdate 的图像设置了一个 onClickListener。

-如果我创建 AppWidget 的实例,它会显示 Uri 文件的最新更改版本。

-如果我单击小部件,我的服务会对文件进行适当的更改(我可以使用文件资源管理器验证),但它不会更新 AppWidget 中显示的图像。

-(最重要的是)如果我删除 AppWidget 并创建一个新的,它会显示图像文件的当前/正确版本。

我知道我的服务可能需要很长时间才能在第一次通过时生效,但它应该会在下一次 onClick/调用 onUpdate 时显示最新的图像。 现在,AppWidget 仅显示在第一次调用 onUpdate 时存在的图像文件的版本。

问题:

什么是刷新 Appwidget 的 RemoteView 内容的正确方法,我在这里的方法中是否遗漏了什么?

感谢您的宝贵时间!

更新:

我已经尝试从 AppWidgetProvider.onReceive() 调用 AppWidgetManager.notifyAppWidgetViewDataChanged() 方法,但在 onUpdate() 之后仍然没有更改 RemoteViews 内容。

public class CCWidgetProvider extends AppWidgetProvider {

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

    // Get all ids
    ComponentName thisWidget = new ComponentName(context,CCWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    for (int widgetId : allWidgetIds)
        {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout04);

        /*
         * it's here that I run a service that changes the content of the file /test/test.png
         */

        RelativeLayout RL_widget = new RelativeLayout(context);
        LayoutInflater inflater = (LayoutInflater)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        RL_widget = (RelativeLayout)inflater.inflate(R.layout.widget_main, null);

        Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/test/test.png");
        remoteViews.setImageViewUri(R.id.IV_widget_image,uri);


        Intent intent = new Intent(context, CCWidgetProvider.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        //PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.IV_widget_image, pendingIntent);

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
 }
} 

最佳答案

我发现有很多东西会使小部件变硬。

A. onUpdate 并不是真正的更新机制

与听起来相反,onUpdate 仅在两种情况下被调用:

  1. 创建小部件时。
  2. 每当更新周期时间(updateMillis 在小部件的 xml 定义文件中定义)过去时。

要点:onUpdate 不会在其他时间调用。 (据我在实践中所见)。

如果你想让小部件在另一个时间更新,有必要创建一个单独的机制,了解小部件和被触发的能力。通常这将是一个 Service,您在 onUpdate 例程中启动它。这可能看起来像:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
{       
    // start the service in case it ain't started yet, this also force a widget update to ensure correct status on them
    Intent intent = new Intent(context, MyService.class);
    intent.putExtra('service.startCode', /*A number to identify what is going on*/);
    context.startService(intent);

    // Everything else is triggered from the service!!
}

服务然后设置小部件的内容,并根据需要更新它们,通过内部计时或通过使用广播机制。

B.你不能真正更新一点小部件

当您创建小部件时创建一个 remoteViews 并在情况发生变化时更新它或其中的 View 似乎是合乎逻辑的。以我的经验,这是无法预料的。当您想更改小部件中的任何内容时,创建一个新的 remoteViews,正确填写它,然后将其分配给小部件。

关于android - AppWidget 刷新 RemoteViews 的 Uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25944639/

相关文章:

java - 在 Java 中打开非默认浏览器

uri - 在 URI 语法中支持 Zeroconf 的最佳方式?

android - 在设置 onClickListener 之前检查 View 是否 == null

android - 如何在 anko 中创建标签 View

android - java.lang.NoSuchMethodError : No static method dist(FFFF)F in class Landroid/util/MathUtils 错误

http - 冒号是否需要在 URI 查询参数中进行编码?

android - 一个 Activity 的两个发射器

android - 应用小部件从数据库获取内容 - 更新问题

android - 应用小部件更新问题 : Failed Binder Transaction, RemoteViews、setImageURI 和方向更改

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