Android的Appwidget TextView中没有更新Android、Appwidget、TextView

2023-09-06 18:36:43 作者:wholehearted(真心)

有我的Andr​​oid小工具一个很奇怪的问题嗨即时消息,香港专业教育学院extensivly看了很多地方,但我似乎无法弄清楚什么是错。基本上即时调用我的窗口小部件,并在成功地抓住这一意图在onrecivie方法的PendingIntent播出。

然而,在onRecive方法,当我尝试设置使用RemoteViews我的组件中的文本,该文本不更新,也不是所谓的任何错误。我重视我的低于code,任何帮助将是巨大的。

谢谢,中号

 包com.android.FirstWidget;进口android.app.PendingIntent;进口android.appwidget.AppWidgetManager;进口android.appwidget.AppWidgetProvider;进口android.content.Context;进口android.content.Intent;进口android.os.Bundle;进口android.util.Log;进口android.view.View;进口android.widget.ImageButton;进口android.widget.RemoteViews;公共类ExampleAppWidgetProvider扩展AppWidgetProvider {    @覆盖    公共无效的onReceive(上下文的背景下,意图意图){        // TODO自动生成方法存根        Log.e(DS,intent.getAction());        Log.e(F,F);        如果(intent.getAction()。包含(1)){            RemoteViews意见=新的RemoteViews(context.getPackageName(),R.layout.wid);            views.setTextViewText(R.id.textView1heyheyhey);            Log.e(fssss,单一小规模设施);        }        super.onReceive(背景下,意图);    }    公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){        最终诠释N = appWidgetIds.length;        //执行的每个应用程序窗口小部件属于此提供这一循环过程        的for(int i = 0; I< N;我++){            INT appWidgetId = appWidgetIds [I]            //创建一个Intent推出为ExampleActivity            意向意图=新意图(背景下,ExampleAppWidgetProvider.class);            intent.setAction(1);            的PendingIntent的PendingIntent = PendingIntent.getBroadcast(上下文,0,意图,0);            //获取为App插件的布局并附加上单击监听器            //到按钮            RemoteViews意见=新的RemoteViews(context.getPackageName(),R.layout.wid);            views.setOnClickPendingIntent(R.id.arrowLeft,的PendingIntent);           views.setOnClickPendingIntent(R.id.arrowRight,的PendingIntent);//views.set            //告诉AppWidgetManager对当前应用程序窗口小部件进行更新            appWidgetManager.updateAppWidget(appWidgetId,意见);        }    }} 

解决方案

您需要几行代码添加到onRecieve方法的末尾....它应该是这样..

 包com.android.FirstWidget;进口android.app.PendingIntent; 进口android.appwidget.AppWidgetManager; 进口android.appwidget.AppWidgetProvider; 进口android.content.Context; 进口android.content.Intent; 进口android.os.Bundle; 进口android.util.Log; 进口android.view.View; 进口android.widget.ImageButton; 进口android.widget.RemoteViews; 公共类ExampleAppWidgetProvider扩展AppWidgetProvider {     @覆盖     公共无效的onReceive(上下文的背景下,意图意图){         // TODO自动生成方法存根         Log.e(DS,intent.getAction());         Log.e(F,F);         RemoteViews意见=新的RemoteViews(context.getPackageName(),R.layout.wid);         如果(intent.getAction()。包含(arrow_left)){             views.setTextViewText(R.id.textView1,左);             Log.e(fssss,单一小规模设施);         }         否则如果(intent.getAction()。包含(ARROW_RIGHT)){             views.setTextViewText(R.id.textView1,右);             Log.e(fssss,单一小规模设施);         }         其他{             super.onReceive(背景下,意图);         }         组件名组件名=新的组件名(背景下,ExampleAppWidgetProvider.class);         AppWidgetManager.getInstance(上下文).updateAppWidget(组件名,意见);     }    公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){        最终诠释N = appWidgetIds.length;        //执行的每个应用程序窗口小部件属于此提供这一循环过程        的for(int i = 0; I< N;我++){            INT appWidgetId = appWidgetIds [I]            //创建一个Intent推出为ExampleActivity            意向意图=新意图(背景下,ExampleAppWidgetProvider.class);            intent.setAction(arrow_left);            的PendingIntent的PendingIntent = PendingIntent.getBroadcast(上下文,0,意图,0);            //获取为App插件的布局并附加上单击监听器            //到按钮            RemoteViews意见=新的RemoteViews(context.getPackageName(),R.layout.wid);            views.setOnClickPendingIntent(R.id.arrowLeft,的PendingIntent);            意图=新意图(背景下,ExampleAppWidgetProvider.class);            intent.setAction(ARROW_RIGHT);            的PendingIntent = PendingIntent.getBroadcast(背景下,0,意向,            views.setOnClickPendingIntent(R.id.arrowRight,的PendingIntent);//views.set            //告诉AppWidgetManager对当前应用程序窗口小部件进行更新            appWidgetManager.updateAppWidget(appWidgetId,意见);        }    }} 
Android AppWidget

另外,虽然我相信你有什么codeD应该工作,但如果没有,你可以尝试把意图行动的按钮在清单文件中。它应该是这样的。

 <意向滤光器>        <作用机器人:名字=android.appwidget.action.APPWIDGET_UPDATE/>        <作用机器人:名字=com.android.FirstWidget.arrow_left/>        <作用机器人:名字=com.android.FirstWidget.arrow_right/>&所述; /意图滤光器> 

我想现在应该工作。我只是碰巧跌入这个问题我自己,发现这里的解决方案:在Android 可点击小工具

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

        } 

    }
}

解决方案

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