Android应用小部件的文本视图不更新

3

你好,我在我的Android小部件中遇到了一个非常奇怪的问题。我已经在许多地方深入研究了,但似乎无法找出问题所在。基本上,在我的小部件中调用pendingintent广播,并在onrecieve方法中成功捕获该意图。

然而,在onRecive方法中,当我尝试使用RemoteViews为我的组件设置文本时,文本不会更新,也不会调用任何错误。我已经附上了我的代码,任何帮助都将是极好的。

谢谢, M

 package com.android.FirstWidget;import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RemoteViews;

public class ExampleAppWidgetProvider extends AppWidgetProvider {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.e("ds",intent.getAction());
        Log.e("f","f");
        if(intent.getAction().contains("1")){

            RemoteViews views =  new RemoteViews(context.getPackageName(), R.layout.wid);
            views.setTextViewText(R.id.textView1,"heyheyhey");
            Log.e("fssss","sssf");
        }
        super.onReceive(context, intent);
    }

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, ExampleAppWidgetProvider.class);
            intent.setAction("1");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid);
            views.setOnClickPendingIntent(R.id.arrowLeft, pendingIntent);
           views.setOnClickPendingIntent(R.id.arrowRight, pendingIntent);
//views.set
            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);

        } 

    }
}
3个回答

7
您需要在onReceive方法的末尾添加几行代码,应该像这样...
 package com.android.FirstWidget;import android.app.PendingIntent;
 import android.appwidget.AppWidgetManager;
 import android.appwidget.AppWidgetProvider;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.ImageButton;
 import android.widget.RemoteViews;

 public class ExampleAppWidgetProvider extends AppWidgetProvider {


     @Override
     public void onReceive(Context context, Intent intent) {
         // TODO Auto-generated method stub
         Log.e("ds",intent.getAction());
         Log.e("f","f");

         RemoteViews views =  new RemoteViews(context.getPackageName(), R.layout.wid);
         if(intent.getAction().contains("arrow_left")){
             views.setTextViewText(R.id.textView1,"left");
             Log.e("fssss","sssf");
         }
         else if(intent.getAction().contains("arrow_right")){
             views.setTextViewText(R.id.textView1,"right");
             Log.e("fssss","sssf");
         }

         else {
             super.onReceive(context, intent);
         }
         ComponentName componentName = new ComponentName(context, ExampleAppWidgetProvider.class);
         AppWidgetManager.getInstance(context).updateAppWidget(componentName, views);
     }

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, ExampleAppWidgetProvider.class);
            intent.setAction("arrow_left");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid);
            views.setOnClickPendingIntent(R.id.arrowLeft, pendingIntent);

            intent = new Intent(context, ExampleAppWidgetProvider.class);
            intent.setAction("arrow_right");
            pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 
            views.setOnClickPendingIntent(R.id.arrowRight, pendingIntent);
//views.set
            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);

        } 

    }
}

尽管我相信你编码的内容应该是有效的,但如果不行,你可以尝试将按钮的意图操作放在清单文件中。应该像这样:
<intent-filter >
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
        <action android:name="com.android.FirstWidget.arrow_left"/>
        <action android:name="com.android.FirstWidget.arrow_right"/>
</intent-filter>

我想现在应该可以工作了。我自己也偶然遇到了这个问题,在这里找到了解决方案:Android中的可点击小部件


我该如何使用您的更新来更新我的代码?我正在尝试从JSON文件中在TextView中显示文本:http://stackoverflow.com/questions/20100117/widget-is-not-updating-after-json-parse - Si8

0
当你重写 onReceive 时,所有意图广播都会调用 onReceive。 onUpdate、onDestroy 等其他 on 上的函数都将不再被调用。
在 onReceive 中获取意图的操作以确定它是什么类型的意图,并确定应该如何处理广播。

是的,我已经获得了正确的操作,然后才调用views.setTextViewText,但是文本视图仍然无法更新。 - Marius
很抱歉,您误解了。您粘贴的代码中的onUpdate方法不会被调用,因为您已经重写了onReceive方法。onReceive方法优先于所有其他on-方法。此外,我所说的“actions”是指这4个操作:http://developer.android.com/guide/topics/appwidgets/index.html#ProviderBroadcasts - Some Noob Student

0
你正在创建RemoteViews并在onReceive中设置textview,但从未调用AppWidgetManager.updateAppWidget(),因此整个操作是完全无意义的,小部件不会更新,因为它没有被发送到remoteviews。
在onUpdate()中,您正确地创建了RemoteViews(但没有设置TextView)并调用了AppWidgetManager.updateAppWidget(),因此这将正确设置按钮pendingintents,但不会设置textview。
解决方案:在设置textview后调用AppWidgetManager.updateAppWidget()。 注意1:还要再次设置pendingintents,否则按钮将失效(如果我没记错的话)。 注意2:每个基于计时器的onupdate都会再次清除textview(如果我没记错的话)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接