【问题标题】:Android Appwidget textview not updatingAndroid Appwidget textview没有更新
【发布时间】:2011-10-20 03:33:48
【问题描述】:

您好,我的 android 小部件有一个非常奇怪的问题,我在很多地方都进行了广泛的查看,但我似乎无法弄清楚出了什么问题。基本上我在我的小部件中调用了一个挂起的意图广播,并在 onrecivie 方法中成功捕获了该意图。

但是在 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);

        } 

    }
}

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    您需要在 onRecieve 方法的末尾添加几行......它应该像这样......

     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>
    

    我想这现在应该可以了。我只是碰巧自己遇到了这个问题,并在这里找到了解决方案:Clickable widgets in android

    【讨论】:

    【解决方案2】:

    当您覆盖 onReceive 时,所有意图广播都会调用 onReceive。 onUpdate、onDestroy 等,所有其他 on 都不再被调用了。

    在 onReceive 中获取 Intent 的操作以确定它是什么类型的 Intent,以及您应该如何处理广播。

    【讨论】:

    • 是的,我得到了正确的操作,然后我才调用views.setTextViewText,但textview仍然不会更新
    • 恐怕你弄错了。您粘贴的代码的 onUpdate 方法将不会在任何时候被调用,因为您已经覆盖了 onReceive。 onReceive 取代所有其他 on 方法。此外,我所说的“动作”是这 4 个动作:developer.android.com/guide/topics/appwidgets/…
    【解决方案3】:

    您正在创建 RemoteViews 并在 onReceive 中设置 textview 但从不调用 AppWidgetManager.updateAppWidget() 所以整个操作完全没有意义,小部件不会更新,因为它没有被发送到 remoteviews。

    在 onUpdate() 中,您正确创建了 RemoteViews(但未设置 TextView)并调用 AppWidgetManager.updateAppWidget(),因此这将正确设置按钮 pendingintents,但未设置 textview。

    解决方法:设置textview后调用AppWidgetManager.updateAppWidget()。 注意1:还要再次设置pendingintents,否则按钮将失效(IIRC)。 注意2:每个基于更新的定时器都会再次清除文本视图(IIRC)。

    【讨论】:

      猜你喜欢
      • 2013-08-09
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多