一、第一个类(MainActivity.java)
package com.hxzy.datasend;
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.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent it1 = new Intent(MainActivity.this, ActivitySecond.class);
startActivity(it1);
new Thread() {
public void run() {
Integer i = 1, j = 1;
int[] array = new int[2];
Intent it2 = new Intent();
for (i = 1; i < 10; i++) {
for (j = 1; j < i; j++) {
array[0] = i;
array[1] = j;
it2.putExtra(publicAttribute.KEY, array);
it2.setAction(publicAttribute.action);
MainActivity.this.sendBroadcast(it2);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}.start();
}
});
}
}
二、第二个类
package com.hxzy.datasend;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
public class ActivitySecond extends Activity {
private final int WHAT = 123;
public TextView textView1, textView2, textView3, textView4;
Handler handler;
MyBroadcastReceiver mBroadcastReceiver;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
textView1 = (TextView) findViewById(R.id.tvSecond1);
textView2 = (TextView) findViewById(R.id.tvSecond2);
textView3 = (TextView) findViewById(R.id.tvSecond3);
textView4 = (TextView) findViewById(R.id.tvSecond4);
mBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter mFilter = new IntentFilter();
mFilter.addAction(publicAttribute.action);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == WHAT) {
int[] array = (int[]) msg.obj;
int i = array[0] * array[1];
textView2.setText("乘数=" + (String.valueOf(array[0])));
textView3.setText("被乘数=" + (String.valueOf(array[1])));
textView4.setText("积=" + (String.valueOf(i)));
}
}
};
ActivitySecond.this.registerReceiver(mBroadcastReceiver, mFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivitySecond.this.unregisterReceiver(mBroadcastReceiver);
}
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int array[] = intent.getIntArrayExtra(publicAttribute.KEY);
Message msg = handler.obtainMessage();// 此方法等同与new Message()
msg.what = WHAT;
msg.obj = array;
handler.sendMessage(msg);
String s = "";
for (int j = 0; j < array.length; j++) {
s = s + array[j] + ",";
}
Log.d("the message is ", s);
}
}
}
三、第三个类(存放静态常量)
package com.hxzy.datasend;
public class publicAttribute {
public static final String KEY="my key";
public static final String action="com.hxzy.datasend";
}