Android AppWidget中的按钮
您好,我正在尝试将按钮添加到我的Appwidget中,但即使在阅读了许多代码之后,我的按钮仍然无法工作 这是我的AppWidgetProvider: 公共类MyWidget扩展了AppWidgetProvider{Android AppWidget中的按钮,android,android-appwidget,Android,Android Appwidget,您好,我正在尝试将按钮添加到我的Appwidget中,但即使在阅读了许多代码之后,我的按钮仍然无法工作 这是我的AppWidgetProvider: 公共类MyWidget扩展了AppWidgetProvider{ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Intent intent = ne
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);
}
}
这是我的服务:
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");
}
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;
}
}
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;
}
}
这只是一个虚拟代码,当你点击按钮时,它应该会改变按钮的文本。它不会
你知道我做错了什么吗?
谢谢。我只是看了一下你的源代码,似乎问题属于逻辑, 更改文本内容后重新创建新的RemoteView对象: 解决方案:注释掉以下代码行:
/* 22/07/2011 DELETE START */
//RemoteViews remoteViews = new RemoteViews(getPackageName(),
// R.layout.widgetlayout);
/* 22/07/2011 DELETE END */
类内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");
}
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;
}
}
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;
}
}
尝试将
onStartCommand
中的代码放入
public void onStart(Intent intent, int startId)
因为您可能会在只使用onStart的旧Android平台上运行代码。将其删除,并将if中的行改为remoteView.etc,而不是remoteView.etc仍然无法工作。