你好我正在嘗試將按鈕添加到我的Appwidget中,但即使在閱讀了很多代碼之後,我的按鈕仍然不起作用。AppWidget中的按鈕
這是我的AppWidgetProvider: 公共類進myWidget擴展的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);
}
}
這裏是爲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;
}
}
這僅僅是應該當改變了按鈕的文本中的虛設碼你點擊它。它沒有。
有關我在做什麼錯的任何想法? 謝謝。
已將其刪除,並將if中的行更改爲remoteView.etc而非remoteViews.etc仍然無法正常工作。 – leonsas