Android Short Polling Solution--countdowntimer+handler

Source: Internet
Author: User

Reprint please specify the original address: http://www.cnblogs.com/ygj0930/p/7657194.html

One: Application scenario

In a swept-code payment scenario such as an automatic vending machine, the client needs to poll the server for a certain amount of time after receiving the payment QR code or making a payment request, to query the payment results for the next answer.

This polling, not a long, timed poll, can only be polled within the time it is waiting to be paid (e.g., within 60 seconds), so the majority of the online polling scheme (Alarmmanager + broadcastreceiver + Service) is cumbersome and not applicable.

We observe that polling within the payment time is "repeating something over time", so it is easy to associate a tool with Android-the timer Countdowntimer.

Second: Polling method

After initiating a payment request, we can start a countdowntimer and let it countdown from a point in time, for example: 60 seconds, and indicate each countdown interval, for example: 1 seconds, so that the countdown is 60 seconds, a few seconds.

Overriding the Ontick () method of the timer, specifying the action to be performed at each countdown, we can make a payment result query request to the server in this method, after receiving the response, depending on the response result, send a message to the UI thread of the Handler,handler according to the different messages, Perform different follow-up actions (jump to different pages, pop-up payment result tips, etc.).

Three: Core code example

Timer =NewCountdowntimer (60000, 1000) {@Override Public voidOnTick (Longmillisuntilfinished) {                           //Poll The payment results in a timer: Query the payment results once per secondRequest Request =Newrequest.builder (). URL ("Server queries the URL of the payment result"). build (); Client.newcall (Request). Enqueue (NewCallback () {@Override Public voidonfailure (call call, IOException e) {//failed to initiate query: Could not successfully initiate query for some reasonE.printstacktrace (); } @Override Public voidOnresponse (call call, Response Response)throwsIOException {String jsondata=NewString (response.body (). Bytes (), "Utf-8"); intRes_code = integer.valueof (Jsondata);//Here I am using 1, 0,-one to show the payment results                        if(Res_code = = 1) {//Payment SuccessMessage message =NewMessage (); Message.what=pay_success;                        Mhandler.sendmessage (message); }Else if(Res_code = =-1) {//Payment FailureMessage message =NewMessage (); Message.what=Pay_fail;                        Mhandler.sendmessage (message);            }                    }                }); } @Override Public voidonfinish () {//countdown to 0 o'clock operation, it is generally considered that the countdown to 0 has not received the results of payment, it is considered that the payment failed, page jump            }        }; Timer.start ();

In handler, different follow-up according to the message (remember: to stop the counter before jumping ), this specific situation is specific, I will not post code.

Android Short Polling Solution--countdowntimer+handler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.