Queue-handling priority information in Android

Source: Internet
Author: User

Requirements: When the interface in processing message A, suddenly received message B, need to immediately display B information, and then continue to display message A, or receive message C, and then display the message a after the message C;

The principle is simple. In a poll, the element in the message list is queried, first the highest priority is processed, and then the higher priority is processed;

Do an object class first

    Private class obj{        int  val;          int Times ;          Public OBJ (intint times ) {            this. val = val;              this. Times = times ;        }    }

Make a list of saved messages again

New Arraylist<fragment_1.obj> ();

To do a polling mechanism again

PrivateRunnable Handlermsg_run =NewRunnable () {@Override Public voidrun () {if(Mlist.size ()! = 0) {OBJ val= Mlist.get (0); inttt =DoSomething (Val); State.settext (Val.val+" + " +TT); if(TT = = 0) {Mlist.remove (0);                    Tv.settext (GetList (mlist)); System.out.println ("-Remove-" + Val.val + ">>" +getList (mlist)); }            } han.postdelayed ( This, 500);   }    }; Han=NewHandler (Looper.getmainlooper ()); Han.post (Handlermsg_run);

Handling Insert List information

    Private voidInsertintrandom) {        intTimes = 5; intNewValue =Random; synchronized(mlist) {if(mlist.size () = = 0) Mlist.add (NewObj (random, Times)); Else if(Mlist.get (0). val! =newvalue) {                    BooleanSHas =false;  for(Obj obj:mlist) {if(Obj.val = =newvalue) {SHas=true;  Break; }                    }                    if(!SHas) {Mlist.add (0,NewObj (NewValue, Times));                    Sort (mlist); } } }  }

To sort the inserted messages

    Private Static Super New Comparator<obj>() {        @Override        publicint  compare (obj arg0, obj arg1) {             return arg0.val- arg1.val;        }    };     Private Static void Sort (list<obj> mlist) {        collections.sort (mlist, comparator);    }

Queue-handling priority information in Android

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.