0

我有一个 android 应用程序小部件,我在其中使用异步任务进行网络调用。现在我想将我的呼叫从异步任务转移到意图服务。doInBackground() 的工作可以在 onHandleIntent() 中完成,但 onPreExecute() 和 onPostExecute() 呢?我在这两种方法中有进度条代码,为我的刷新按钮提供旋转效果。我应该把代码放在意图服务的哪里?

更新

public class StoreWidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        super.onUpdate(context, appWidgetManager, appWidgetIds);
        updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_main_layout1);
        mContext = context;
        mProgressBar = new ProgressBar(context);

        Intent localIntent = new Intent(context,StoreWidgetService.class);

        context.startService(localIntent);
//      try {
//          fetchTask.execute().get();
//      } catch (InterruptedException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      } catch (ExecutionException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }

        MyCurrentLocation(mContext);
        imageLoader = new ImageLoader(mContext);

        updateViews.setOnClickPendingIntent(R.id.next, next_buildButtonPendingIntent(context));

        updateViews.setOnClickPendingIntent(R.id.back, back_buildButtonPendingIntent(context));

        updateViews.setOnClickPendingIntent(R.id.refresh, refresh_buildButtonPendingIntent(context));

//----These commented as they use context and this also gets null when killed----
        //updateViews.setOnClickPendingIntent(R.id.outer_text_view, merchant_buildButtonPendingIntent(context));

        //updateViews.setOnClickPendingIntent(R.id.check_in, checkin_buildButtonPendingIntent(context));

        //updateViews.setOnClickPendingIntent(R.id.image_logo_id, pIcon_buildButtonPendingIntent(context));

        pushWidgetUpdate(context,updateViews);
    }


//my button listeners: next and prev
//--not shown here---


    public static void pushWidgetUpdate(Context context,RemoteViews views){
        System.out.println("Inside pushwidget");
        context = mContext;

        if(context!=null){
            ComponentName myWidget=new ComponentName(context, StoreWidgetProvider.class);
            AppWidgetManager manager=AppWidgetManager.getInstance(context);
            manager.updateAppWidget(myWidget, views);
        }
        }
    }

    public static class StoreWidgetService extends IntentService implements ServerRequestEnvironment{

        public StoreWidgetService() {
            super("StoreWidgetService");
            // TODO Auto-generated constructor stub
        }

//      protected void onPreExecute(){
//          
//          updateViews.setViewVisibility(R.id.refresh, View.GONE);
//          updateViews.setViewVisibility(R.id.progress, View.VISIBLE);
//          pushWidgetUpdate(mContext,updateViews);
//      }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId){
            super.onStartCommand(intent, flags, startId);
            return START_REDELIVER_INTENT;
        }
        @Override
        public void onHandleIntent(Intent intent) {


            //my data fetching done with all network calls 

        }

//      protected void onPostExecute(Store storeObj) {
//
//          updateViews.setViewVisibility(R.id.progress, View.GONE);
//          updateViews.setViewVisibility(R.id.refresh, View.VISIBLE);
//          
//
//          pushWidgetUpdate(mContext,updateViews);
//      }


        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }

    }
}   
4

1 回答 1

0

您必须Handler在主 UI 线程中注册一个,然后将其传递HandlerIntentService. 通过这个处理程序,您可以将Runnables 排队等待在 UI 线程上执行。由于AsyncTask由 a 支持ThreadPool,我认为这AsyncTask确实是在做的事情。

您也可以尝试使用该函数runOnUiThread()(但我认为它只能由Activity对象调用)。

看看这里以获得更深入的理解:

http://developer.android.com/training/multiple-threads/communicate-ui.html

于 2013-07-21T19:46:43.440 回答