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