【问题标题】:should I use IntentService or Service for UI update?我应该使用 IntentService 还是 Service 进行 UI 更新?
【发布时间】:2013-06-06 03:16:28
【问题描述】:

我想知道在 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);

}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-03
      • 2023-03-11
      • 2013-02-16
      • 2010-09-19
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      相关资源
      最近更新 更多