我应该使用IntentService或Service进行UI更新吗?

时间:2013-06-10 12:53:43

标签: android android-intent android-widget android-service

我想知道在Android小部件中使用IntentService类而不是Service类来管理UI更新(以及注册onClickListener)是否有任何区别。以下是我分别使用onHandleIntentonStartCommand的代码。

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);

}

1 个答案:

答案 0 :(得分:0)

顺便说一句,如果你想在服务调用期间更新UI线程,那么就不可能。但Android提供了AsyncTasks来处理这种情况。 http://developer.android.com/reference/android/os/AsyncTask.html