<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义Thread继承Thread" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义Runnable实现Runnable" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="定时更新UI界面,Handler分发Runnable对象" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="定时更新UI界面,Handler分发Message对象" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
</LinearLayout>
package com.chengdong.su.threaddemo;
import com.chengdong.su.threaddemo.util.MyRunnable;
import com.chengdong.su.threaddemo.util.MyThread;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
/** TAG */
private final String TAG = getClass().getSimpleName();
/** the object of the button */
private Button mButton;
/** the object of the button */
private Button mButton2;
/** the object of the button */
private Button mButton3;
/** the object of the button */
private Button mButton4;
/** the object of the TextView */
private TextView mTextView;
/** 计数 */
private int mCount = 0;
/** 标志 */
private int MESSAGE_FLAG = 1;
/**
* Handler分发Runnable对象的方式
*/
private Handler mHandler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
mCount++;
mHandler.postDelayed(runnable, 1000);
mTextView.setText(mCount + "");
}
};
/***
* Handler分发Message对象的方式
*/
Handler mHandler2 = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
mTextView.setText("Handler分发Message对象的方式");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化组件对象
*/
private void initView() {
mButton = (Button) findViewById(R.id.btn);
mButton2 = (Button) findViewById(R.id.btn2);
mButton3 = (Button) findViewById(R.id.btn3);
mButton4 = (Button) findViewById(R.id.btn4);
mButton.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mButton4.setOnClickListener(this);
mTextView = (TextView) findViewById(R.id.tv);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn: {
// 方法一:继承的方式:自定义Thread继承Thread,开启一个新的线程
new MyThread().start();
break;
}
case R.id.btn2: {
// 方法二:实现的方式:implement Runnable
new Thread(new MyRunnable()).start();
break;
}
// 方法三:handler分发Runnable对象:定时更新UI界面 提交计划任务马上执行
case R.id.btn3: {
// Handler分发Runnable对象
mHandler.post(runnable);
break;
}
// 方法四:Handler分发Message对象 ,定时更新UI界面 提交计划任务马上执行
case R.id.btn4: {
// 不推荐这种方式
// Message msg = new Message();
// 推荐使用这种获取对象的方式:从消息池中获得可用的Message对象
Message msg = Message.obtain();
msg.what = MESSAGE_FLAG;
mHandler2.sendMessage(msg);
break;
}
default:
break;
}
}
}
package com.chengdong.su.threaddemo.util;
import android.util.Log;
/***
* 自定义一个MyRunnable线程
*
* @author scd
*
*/
public class MyRunnable implements Runnable {
public MyRunnable() {
super();
}
/** TAG */
private final String TAG = getClass().getSimpleName();
@Override
public void run() {
for (int i = 0; i < 20; i++) {
Log.e(TAG, Thread.currentThread().getName() + ",实现的方法" + i);
}
}
}
package com.chengdong.su.threaddemo.util;
import android.util.Log;
/***
* 自定义一个线程
*
* @author scd
*
*/
public class MyThread extends Thread {
public MyThread() {
super();
}
/** TAG */
private final String TAG = getClass().getSimpleName();
@Override
public void run() {
super.run();
for (int i = 0; i < 10; i++) {
Log.e(TAG, Thread.currentThread().getName() + ",继承Thread类:" + i);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有