android - Call AsyncTask in the onRecive -


i need call asynctask function since onreceive(). problem when call function, different textviews must change in onpostexecute(), don't change it!

this code:

@override public void onreceive(context context, intent intent) {     // todo auto-generated method stub     super.onreceive(context, intent);      if (sync_clicked.equals(intent.getaction())) {          appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(context);         int appwidgetid = intent.getintextra(appwidgetmanager.extra_appwidget_id,                 appwidgetmanager.invalid_appwidget_id);               remoteviews remoteviews;             componentname watchwidget;              remoteviews = new remoteviews(context.getpackagename(), r.layout.widget);             watchwidget = new componentname(context, widget.class);              remoteviews.settextviewtext(r.id.textview56, "actualizando");              remoteviews views = new remoteviews(context.getpackagename(), r.layout.widget);             new longoperation(views, appwidgetid, appwidgetmanager).execute("myteststring"); //calling asynctask              appwidgetmanager.updateappwidget(watchwidget, remoteviews);      } }  protected pendingintent getpendingselfintent(context context, string action) {         intent intent = new intent(context, getclass());         intent.setaction(action);         return pendingintent.getbroadcast(context, 0, intent, 0);     } 

and asynctask. part of code use , works when call sice onupdate:

public class longoperation extends asynctask<string, void, string> {         private remoteviews views;         private int widgetid;         private appwidgetmanager widgetmanager;          public longoperation(remoteviews views, int appwidgetid, appwidgetmanager appwidgetmanager){             this.views = views;             this.widgetid = appwidgetid;             this.widgetmanager = appwidgetmanager;          }          @override         public void onpreexecute() {             super.onpreexecute();         }          @override         public string doinbackground(string... params) {             try {                 ....             } catch (exception e) {                 ....             }              return temperatura;         }          @override         public void onpostexecute(string result) {             views.settextviewtext(r.id.textview66, result+ "Âșc ");              widgetmanager.updateappwidget(widgetid, views);         }     } 

i think problem in appwidgetid can't solve...

thanks,

marc

the use of asynctask in broadcast bad practice, because android may kill process in onreceive() if there no active service or activity, , no gurantee return.

in case, official documentation recommends intentservice:

"the specific constraint on broadcastreceiver execution time emphasizes broadcast receivers meant do: small, discrete amounts of work in background such saving setting or registering notification. other methods called in ui thread, applications should avoid potentially long-running operations or calculations in broadcast receiver. instead of doing intensive tasks via worker threads, application should start intentservice if potentially long running action needs taken in response intent broadcast."


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -