Android Appwidget textview not updating

╄→гoц情女王★ 提交于 2019-12-07 11:56:54

问题


Hi im having a very weird problem with my android widget, ive looked extensivly in many places but I cant seem to figure out whats wrong. basically im calling a pendingintent broadcast in my widget and in sucessfully catching that intent in the onrecivie method.

However in the onRecive method, when I try to set the text using RemoteViews for my component, the text does not update nor is any error called. I have attached my code below, any help would be great.

Thanks, 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);

        } 

    }
}

回答1:


You need to add couple of lines to the end of onRecieve method.... it should go like this..

 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);

        } 

    }
}

Also though I believe what you have coded should work, but if not you can try putting the intent action for the buttons in the manifest file. It should go like this

<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>

I guess this should work now. I just happen to stumble into this problem myself, and found solution here : Clickable widgets in android




回答2:


When you override onReceive, all intent broadcasts are made to call onReceive. onUpdate, onDestroy, etc, all the other on's are not called anymore.

Get the action of the intent in onReceive to determine what type of intent it is, and what you should do with the broadcast.




回答3:


You are creating the RemoteViews and setting the textview in the onReceive but never call AppWidgetManager.updateAppWidget() so this whole action is totally pointless, the widget will not update because it's not being sent the remoteviews.

In onUpdate() you correctly create RemoteViews (but not set the TextView) and call AppWidgetManager.updateAppWidget(), so this will correctly set the button pendingintents, but not set the textview.

Solution: call AppWidgetManager.updateAppWidget() after setting the textview. Note 1: also set the pendingintents again, otherwise the button will be dead (IIRC). Note 2: every timerbased onupdate will clear the textview again (IIRC).



来源:https://stackoverflow.com/questions/6944553/android-appwidget-textview-not-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!