实训6 Handle消息传递
一、【实训目的】
(1) Handle消息传递的原理
(2) Handle消息传递的实际应用
-
【实训步骤和要求】
-
生成动态随机数
1).使用布局文件实现以下界面,使用Handle消息传递机制,在此界面的基础上,添加两个按钮“生成随机数”“暂停”;完成以下功能:
点击“生成随机数”按钮主线程和子线程生成动态随机数;
点击“暂停”按钮主线程和子线程停止生成动态随机数;
2).给出了布局代码文件,完成MainActivity.java中的代码;
Activity_main.xml文件代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/myText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="20sp" />
<TextView
android:id="@+id/myText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="20sp" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="生成随机数"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="暂停"/>
</LinearLayout>
MainActivity.java中的代码如下:
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView myText1, myText2,myText3;
private Button btn1,btn2;
boolean isstop;
Thread thread;
Handler handler = new Handler() {
public void handleMessage(Message msg) { //信息处理的方法
if(msg.what==0x11){
double rand1=Math.random();
double rand2=Math.random();
myText2.setText("子线程随机数是:"+rand2);
myText1.setText("父线程随机数是:"+rand1);
}else if(msg.what==0x22){
thread.stop();
}
super.handleMessage(msg);
}
};
Runnable runnable=(new Runnable() {
@Override
public void run() {
//TODO Auto-genetated method stub
while (isstop){
try {
while (true) {
Thread.sleep(300);
handler.sendEmptyMessage(0x11);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText1 = (TextView) findViewById(R.id.myText1);
myText2 = (TextView) findViewById(R.id.myText2);
btn1=(Button) findViewById(R.id.btn1);
btn2=(Button) findViewById(R.id.btn2);
double rand = Math.random();
myText1.setText("主线程随机数为:" + rand);
myText2.setText("随机数为:" + rand);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0){
// TODO Auto-generated method stub
//主线程和子线程的消息传递
isstop=true;
if(thread==null){
thread = new Thread(runnable);
thread.start();
}
}
});
btn2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0){
// TODO Auto-generated method stub
isstop=false;
if(thread!=null){
thread.interrupt();
thread=null;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
运行结果截图:
2、写一个程序验证Activity的生命周期,输出如下所示结果:
MainActivity.java中的代码如下:
@Override
protected void onDestroy() {
System.out.println("MainActivity's onDestroy");
super.onDestroy();
}
@Override
protected void onPause() {
System.out.println("MainActivity's onPause");
super.onPause();
}
@Override
protected void onRestart() {
System.out.println("MainActivity's onRestart");
super.onRestart();
}
@Override
protected void onResume() {
System.out.println("MainActivity's onResume");
super.onResume();
}
@Override
protected void onStart() {
System.out.println("MainActivity's onStart");
super.onStart();
}
@Override
protected void onStop() {
System.out.println("MainActivity's onStop");
super.onStop();
}
运行结果截图:
附注:该专栏是博主上学时的实训项目,可供访客练习与参考。代码质量不是很好,但能实现,仅供参考!