• Android 实战之UI线程和Worker线程交互


    哈哈,博文取了个比较霸气的名字,大家不都喜欢这样忽悠人吗 呵呵!

    好了,现在就是很简单的点击查询,然后这个查询有点花时间,不想见面出现假死现象,所以在另外的线程进行查询。

    好了,代码在此:

    package com.example.gulanfinddemo;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Looper;
    import android.os.Message;
    import android.util.Log;
    import android.widget.EditText;
    
    public class FindResActivity extends Activity {
    
    	private MyHandler handler;
    	private Boolean isKeywordFindBoolean = false;
    	EditText resText;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_find_res);
    		
    		resText = (EditText)findViewById(R.id.ResText);
    		
    		Log.v("FindResActivity threadId:", String.valueOf(Thread.currentThread().getId()));
    		
    		Intent intent = getIntent();
    		String findReString;
    		
    		if (null != intent.getStringExtra(MainActivity.suraAyaFindStr)) {
    			findReString = "按章节查询";
    			isKeywordFindBoolean = false;
    		}
    		else {
    			findReString = "按关键字查询";
    			isKeywordFindBoolean = true;
    		}
    		
    		Looper looper = Looper.myLooper();
    		handler = new MyHandler(looper);
    		
    		new findContentThread(handler, findReString).start();
    		
    		//resText.setText(findReString);
    	}
    	
    	private class MyHandler extends Handler{
    		public MyHandler(Looper looper){
    			super(looper);
    		}
    		public void handleMessage(Message msg){
    			if (msg.what == 0) {
    				Bundle bundle = msg.getData();
    				resText.setText(bundle.getString("res"));
    			}
    		}
    	}
    	
    }
    

      看出来了,重载了handler,在

    handleMessage里会进行UI的更新。
    package com.example.gulanfinddemo;
    
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    
    public class findContentThread extends Thread{
    
    	private String queryKeyString;
    	private Handler handler;
    	
    	public findContentThread(Handler sendHandler,String string){
    		queryKeyString = string;
    		handler = sendHandler;
    	}
    	
    	public void run(){
    		Log.v("findContentThread threadId:", String.valueOf(Thread.currentThread().getId()));
    		Message msgMessage = new Message();
    		Bundle bundle = new Bundle();
    		bundle.putString("res", queryKeyString);
    		msgMessage.setData(bundle);
    		msgMessage.what = 0;
    		handler.sendMessage(msgMessage);
    		super.run();
    	}
    }
    

      在这里用了扩展Thread的方法,处理之后handler会传回消息,之后就在handler里处理消息了。

    然后里面打印了线程ID,从其中可以确定的确是在不同的线程里执行的。

  • 相关阅读:
    【Unity】自定义编辑器窗口——拓展编辑器功能
    【Unity】AssetBundle的使用——打包/解包
    【Unity】使用Resources类管理资源
    【Unity】使用AssetDatabase编辑器资源管理
    【Unity】协程Coroutine及Yield常见用法
    【Unity】制作简易定时器(Timer)
    python3使用csv模块读写csv文件
    Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题
    MongoDB服务无法启动,发生服务特定错误:100
    ValueError: update only works with $ operators
  • 原文地址:https://www.cnblogs.com/xiangshancuizhu/p/3375739.html
Copyright © 2020-2023  润新知