Android Appwidget textview not updating
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 a开发者_C百科ndroid.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
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.
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).
精彩评论