我想知道在Android小部件中使用IntentService
类而不是Service
类来管理UI更新(以及注册onClickListener
)是否有任何区别。以下是我分别使用onHandleIntent
和onStartCommand
的代码。
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
for (int widgetId : allWidgetIds) {onClickListener
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.main);
// Register an onClickListener
Intent clickIntent = myIntent(this.getApplicationContext());
PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.text, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
答案 0 :(得分:0)
顺便说一句,如果你想在服务调用期间更新UI线程,那么就不可能。但Android提供了AsyncTasks来处理这种情况。 http://developer.android.com/reference/android/os/AsyncTask.html。