How To Get Int Value From Asynctask
Solution 1:
In your onPostExecute...
Before the // Dismiss the progress dialog
//compare your status/integer value over here and depending on that, while dismissing the dialogif(pDialog.isShowing() && compareValue()){
pDialog.dismiss();
//show a custom dialog maybe or depending on your requirement carry on with your implementation
} else {
//show the message to the user (not signed in or invalid credentials dialog etc)
}
Solution 2:
You have several choices:
Use a
Handler
. You define it within yourActivity
, pass it to yourAsyncTask
via a new function likesetHandler(Handler my_handler)
and send a message from it.Use a local
BroadcastReceiver
. If you don't know how to do it, check this link, it's a great example and well explained.
---- EDIT ----
This would be an example of a Handler
. In your Activity
define something like this:
publicclassMyRcvDataextendsHandler {
@OverridepublicvoidhandleMessage(Message msg) {
Integeryour_int= msg.arg1;
Log.d("MyClass", "My Integer is: " + your_int);
}
}
Define an instance of that Handler. Save it as a class-wide instance, so you can access it whenever you need:
publicclassLoginScreenextendsActivityimplementsOnClickListener {
... Declarations ...
MyRcvDatamyHandler=newMyRcvData();
In your AsyncTask
, add a public method to set the handler, so the AsyncTask
knows which handler to use to send messages.
publicvoidsetHandler(Handler h) { myHandler = h; }
Of course, that means you need to store the Handler
in your AsyncTask
as well. Declare it that way: Handler myHandler;
You have to call this method prior to calling execute()
, so your MyRcvData
has to be initialized prior to calling execute()
too.
Now you just have to send your "message". In your AsyncTask
, whenever you need to send your Integer
to your Activity
, you would do something like this:
Messagemsg= Message.obtain();
msg.arg1 = your_integer;
msg.sendMessage();
That will fire your handleMessage()
above. I strongly recommend having a look at both the Message class and Handler class for further info.
Post a Comment for "How To Get Int Value From Asynctask"