Android Appwidget textview不更新

时间:2011-08-04 15:51:36

标签: android android-widget

嗨,即时通讯我的Android小部件有一个非常奇怪的问题,我在很多地方看起来很好看,但我似乎无法弄清楚什么是错的。基本上我在我的小部件中调用pendingintent广播并成功地在onrecivie方法中捕获该意图。

但是在onRecive方法中,当我尝试使用RemoteViews为我的组件设置文本时,文本不会更新,也不会调用任何错误。我在下面附上了我的代码,任何帮助都会很棒。

谢谢, 中号

 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 个答案:

答案 0 :(得分:7)

你需要在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操作。它应该是这样的

<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

答案 1 :(得分:0)

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

获取onReceive中意图的操作,以确定它的意图类型,以及您应该如何处理广播。

答案 2 :(得分:0)

您正在创建RemoteView并在onReceive中设置textview但从不调用AppWidgetManager.updateAppWidget(),因此整个操作完全没有意义,窗口小部件不会更新,因为它没有被发送到远程视图。

在onUpdate()中你正确创建了RemoteViews(但没有设置TextView)并调用AppWidgetManager.updateAppWidget(),所以这将正确设置按钮pendingintents,但不设置textview。

解决方案:在设置textview后调用AppWidgetManager.updateAppWidget()。 注1:再次设置pendingintents,否则按钮将死(IIRC)。 注2:每个基于定时器的onupdate将再次清除textview(IIRC)。