package com.lixu.caihong;

import com.lixu.caihong.MainActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private String action = "com.lixu";
    private static String KEY = "XX";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Button btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Activityb.class);
                startActivity(intent);
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        int[] color1 = { 0xffB71C1C, 0xfff44336, 0xffEEFF41, 0xff00C853, 0xff4CAF50, 0xff03A9F4,
                                0xff6200EA };
                        int a = 0;
                        int b = 1;
                        int c = 2;
                        int d = 3;
                        int e = 4;
                        int f = 5;
                        int g = 6;
                        while (true) {
                            Intent intent = new Intent();
                            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 block
                                e1.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();
            }
        });
    }
}
package com.lixu.caihong;

import java.util.Random;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class Activityb extends Activity {
    private String action = "com.lixu";
    private static String KEY = "XX";
    private static final int WHAT = 1;
    Handler handler;
    Mybroadcast mMybroadcast;
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    TextView tv7;

    @Override
    protected void onCreate(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 = new Mybroadcast();//创建广播
        IntentFilter filter = new IntentFilter();
        filter.addAction(action);//设置过滤器
        registerReceiver(mMybroadcast, filter);
        handler = new Handler() {
            public void handleMessage(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]);
                }
            }

        };

    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mMybroadcast);
        super.onDestroy();
    }

    public class Mybroadcast extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            int[] colors = intent.getIntArrayExtra(KEY);
            Message msg = handler.obtainMessage();
            msg.what = WHAT;
            msg.obj = colors;
            handler.sendMessage(msg);
        }
    }
}

 运行效果:

posted on 2015-11-20 09:25  0代码狂人0  阅读(310)  评论(0编辑  收藏  举报