PackageCom.lixu.caihong;Importcom.lixu.caihong.MainActivity;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {PrivateString action = "Com.lixu"; Private StaticString KEY = "XX"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Button BTN1=(Button) Findviewbyid (R.ID.BTN1); Btn1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (mainactivity. This, Activityb.class); StartActivity (Intent); NewThread (NewRunnable () {@Override Public voidrun () {int[] Color1 = {0xffb71c1c, 0xfff44336, 0xffeeff41, 0xff00c853, 0XFF4CAF50, 0XFF03A9F4, 0xff6200ea }; intA = 0; intb = 1; intc = 2; intD = 3; intE = 4; intF = 5; intG = 6; while(true) {Intent Intent=NewIntent (); Intent.setaction (action); int[] color ={Color1[a], color1[b], color1[c], color1[d], color1[e], color1[f], color1[g]}; Intent.putextra (KEY, color); Mainactivity. This. Sendbroadcast (Intent); Try{Thread.Sleep (200); } Catch(interruptedexception E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } A++; b++; C++; D++; E++; F++; G++; if(A = =color1.length) {a= 0; } Else if(b = =color1.length) {b= 0; } Else if(c = =color1.length) {c= 0; } Else if(d = =color1.length) {d= 0; } Else if(E = =color1.length) {e= 0; } Else if(f = =color1.length) {f= 0; } Else if(g = =color1.length) {g= 0; }}}). Start (); } }); }}
PackageCom.lixu.caihong;ImportJava.util.Random;Importandroid.app.Activity;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.Window;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classActivitybextendsActivity {PrivateString action = "Com.lixu"; Private StaticString KEY = "XX"; Private Static Final intwhat = 1; Handler Handler; Mybroadcast Mmybroadcast; TextView TV1; TextView TV2; TextView TV3; TextView TV4; TextView TV5; TextView TV6; TextView TV7; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.LAYOUT.ACTIVITYB); TV1=(TextView) Findviewbyid (R.ID.TV1); TV2=(TextView) Findviewbyid (R.ID.TV2); TV3=(TextView) Findviewbyid (R.ID.TV3); TV4=(TextView) Findviewbyid (R.ID.TV4); TV5=(TextView) Findviewbyid (R.ID.TV5); TV6=(TextView) Findviewbyid (R.ID.TV6); TV7=(TextView) Findviewbyid (R.ID.TV7); Mmybroadcast=NewMybroadcast ();//Create a broadcastIntentfilter filter =NewIntentfilter (); Filter.addaction (action);//Set FilterRegisterreceiver (mmybroadcast, filter); Handler=NewHandler () { Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); if(Msg.what = =What ) { int[] color = (int[]) msg.obj; Tv1.setbackgroundcolor (color[0]); Tv2.setbackgroundcolor (color[1]); Tv3.setbackgroundcolor (color[2]); Tv4.setbackgroundcolor (color[3]); Tv5.setbackgroundcolor (color[4]); Tv6.setbackgroundcolor (color[5]); Tv7.setbackgroundcolor (color[6]); } } }; } @Overrideprotected voidOnDestroy () {unregisterreceiver (mmybroadcast); Super. OnDestroy (); } Public classMybroadcastextendsBroadcastreceiver { Public voidOnReceive (Context context, Intent Intent) {int[] Colors =Intent.getintarrayextra (KEY); Message msg=Handler.obtainmessage (); Msg.what=what ; Msg.obj=colors; Handler.sendmessage (msg); } }}
Operating effect:
Write a rainbow dynamic effect with dynamic broadcast mechanism!