Hello I'm trying to add buttons to my Appwidget, but even after reading many codes, my buttons aren't working.
This is my AppWidgetProvider: public class MyWidget extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Intent intent = new Intent(context, MyService.class);
intent.setAction("0");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
views.setOnClickPendingIntent(R.id.button_widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
}
And here is MyService:
public class MyService extends Service {
@Override
public int o开发者_如何学运维nStartCommand(Intent intent, int flags, int startId) {
String command = intent.getAction();
int[] appWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
RemoteViews remoteView = new RemoteViews(getApplicationContext()
.getPackageName(), R.layout.widgetlayout);
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(getApplicationContext());
if (command.equals("0")) {
remoteViews.setTextViewText(R.id.button_widget, "TextChanged");
}
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.widgetlayout);
appWidgetManager.updateAppWidget(appWidgetIds, remoteView);
super.onStart(intent, startId);
return 0;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
This is just a dummy code that is supposed to changed a button's text when you click it. It doesn't.
Any ideas about what I'm doing wrong? Thanks.
i just take a look in your source code, it seem problem belong logic, your re-create new RemoteView object after your change text content:
Solution: Comment out following line of code:
/* 22/07/2011 DELETE START */
//RemoteViews remoteViews = new RemoteViews(getPackageName(),
// R.layout.widgetlayout);
/* 22/07/2011 DELETE END */
In class MyService:
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String command = intent.getAction();
int[] appWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
RemoteViews remoteView = new RemoteViews(getApplicationContext()
.getPackageName(), R.layout.widgetlayout);
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(getApplicationContext());
if (command.equals("0")) {
remoteViews.setTextViewText(R.id.button_widget, "TextChanged");
}
/* 22/07/2011 DELETE START */
//RemoteViews remoteViews = new RemoteViews(getPackageName(),
// R.layout.widgetlayout);
/* 22/07/2011 DELETE END */
appWidgetManager.updateAppWidget(appWidgetIds, remoteView);
super.onStart(intent, startId);
return 0;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Try to put your code from onStartCommand
into
public void onStart(Intent intent, int startId)
as you might run your code on older Android platforms that only use onStart.
精彩评论