标签:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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="com.dragon.testfuction.Main">
<ImageView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
package com.dragon.testfuction;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class Main extends AppCompatActivity {
// 定义图片显示ID
int[] imageIds = new int[]{
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four
};
int currentImageId = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView show = (ImageView) findViewById(R.id.show);
final Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg){
// 检查消息发送来源,如果是本程序发送的
if(msg.what == 0x1233){
// 动态修改图片
show.setImageResource(imageIds[currentImageId++%imageIds.length]);
}
}
};
//定义一个定时器,周期性的执行指定任务
new Timer().schedule(new TimerTask(){
@Override
public void run(){
//在子线程中拿到父线程中创建的handle对象,通过该对象来向父线程的消息队列发送消息(通过这种方式,可以在其它子线程中与主线程通信来由UI线程更新界面)
myHandler.sendEmptyMessage(0x1233);
}
},0,1200);
}
}
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.dragon.testfuction.Main">
<EditText
android:id="@+id/etNum"
android:inputType="number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="please input upper number"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="cal"
android:text="calculate"/>
</LinearLayout>
package com.dragon.testfuction;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class Main extends AppCompatActivity {
static final String UPPER_NUM = "upper";
EditText etNum;
CalThread calThread;
// 定义一个线程类
class CalThread extends Thread {
public Handler mHandler;
public void run(){
// 创建looper对象,每一个线程使用Handle都要有一个looper对象
Looper.prepare();
// 子线程中定义handler获取处理消息
mHandler = new Handler(){
// 定义处理信息的方法
@Override
public void handleMessage(Message msg){
if(msg.what == 0x123){
int upper = msg.getData().getInt(UPPER_NUM);
List<Integer> nums = new ArrayList<Integer>();
outer:
// 质数也是素数,除了1和它本身外,不能被其它整除
for(int i =2;i <=upper;i++){
for(int j=2; j<= Math.sqrt(i);j++){
// 如果可以整除,说明不是质数
if(i!=2 && i%j==0){
continue outer;
}
}
nums.add(i);
}
// 用Toast显示所有统计出来的质数
Toast.makeText(Main.this,nums.toString(),Toast.LENGTH_LONG).show();
}
}
};
Looper.loop();//启动looper
}
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etNum = (EditText) findViewById(R.id.etNum);
calThread = new CalThread();
calThread.start();//启动新线程
}
//按钮事件点击处理函数
public void cal(View source){
// 创建消息
Message msg = new Message();
msg.what = 0x123;
Bundle bundle = new Bundle();
bundle.putInt(UPPER_NUM,Integer.parseInt(etNum.getText().toString()));
msg.setData(bundle);
// 在主线程中想新线程中的Handler发送消息
calThread.mHandler.sendMessage(msg);//在主线程中发送消息
}
}
/** Initialize the current thread as a looper.
* This gives you a chance to create handlers that then reference
* this looper, before actually starting the loop. Be sure to call
* {@link #loop()} after calling this method, and end it by calling
* {@link #quit()}.
*/
public static void prepare() {
prepare(true);
}
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
1.http://www.runoob.com/w3cnote/android-tutorial-handler-message.html
2.http://www.nljb.net/default/Android%E4%B9%8BHandler%E6%B6%88%E6%81%AF%E4%BC%A0%E9%80%92%E6%9C%BA%E5%88%B6%E4%BD%BF%E7%94%A8/
3.http://www.itmmd.com/201502/575.html
4.http://www.kancloud.cn/digest/tttkkk/125281
android studio for android learning (十九 ) 最新Handler消息传递机制全解
标签:
原文地址:http://blog.csdn.net/yywan1314520/article/details/51932650