我有一个Android应用程序小部件,我在网络调用中使用了异步任务。我想将我的调用从asynctask转移到intent服务。 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;
}
}
}
答案 0 :(得分:0)
您必须在主UI线程中注册Handler
,然后将此Handler
传递给IntentService
。通过此处理程序,您可以将Runnable
队列在UI线程上执行。由于AsyncTask
由ThreadPool
支持,我认为这是AsyncTask
确实在做的事情。
您也可以尝试使用函数runOnUiThread()
(但我认为它只能由Activity
对象调用。)
看看这里有更深入的了解:
http://developer.android.com/training/multiple-threads/communicate-ui.html