[英]should I use IntentService or Service for UI update?
我想知道在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);
}
顺便说一句,如果您想在服务调用期间更新UI线程,则不可能。 但是Android提供了AsyncTasks来处理这种情况。 http://developer.android.com/reference/android/os/AsyncTask.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.