I'm wondering whether it makes any difference to use the IntentService
class rather than the Service
class in an android widget for managing UI updates (and registering onClickListener
). Below is my code that I use onHandleIntent
and onStartCommand
respectively.
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);
}
By the way if you want to update UI threads during service calling it is not possible. But Android provides AsyncTasks to handle this kind of scenario. http://developer.android.com/reference/android/os/AsyncTask.html .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.