gpt4 book ai didi

Android Appwidget textview不更新

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:05 25 4
gpt4 key购买 nike

您好,我的 Android 小部件出现了一个非常奇怪的问题,我仔细查看了很多地方,但我似乎无法弄清楚哪里出了问题。基本上我在我的小部件中调用了一个 pendingintent 广播,并在 onrecivie 方法中成功地捕获了该 Intent 。

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

}

}
}

最佳答案

您需要在 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 操作放在 list 文件中。应该是这样的

<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

关于Android Appwidget textview不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6944553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com