Note that there are some explanatory texts on larger screens.

plurals
  1. POmapping from asynctask to intent service
    text
    copied!<p>I have an android app widget where I have used async task for my network calls. NOw I want to shift my calls from asynctask to intent service. doInBackground() 's work can be done in onHandleIntent() but what about onPreExecute() and onPostExecute(). I have the progress bar code in these 2 methods to give a spinning effect for my refresh button. Where should I put the code for this in intent service? </p> <p>UPDATE</p> <pre><code>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; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload