现在的位置: 首页 > 综合 > 正文

android之Handler,activity,service总结

2013年08月18日 ⁄ 综合 ⁄ 共 3057字 ⁄ 字号 评论关闭

1、Handler学习

由于有时要进行哪些耗时操作,如果这些操作放在主线程的话,容易导致程序假死,最后结果是系统崩溃。因此要进行某些耗时操作时,将其放在子线程进行,这样就涉及如何解决子线程与主线程之间通信问题(消息队列)。

Handler就是为了解决这个问题而存在的。当你处理某个耗时操作时,你将其放在子线程,子线程处理完后,将其结果放在消息队列中,然后主线程从消息队列中读取子线程处理结果,最后根据结果更新主线程。这样就是Handler处理整个流程。

Handler主要有两种方式实现主线程与子线程通信:

一、通过post(Runnable对象)将Runnable对象加到消息队列中,然后主线程从消息队列中读取Runnable对象,根据结果更新主线程。

package com.test.ui;

import android.app.Activity;
import android.os.Handler;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
 

    Button btn1,btn2;
    ProgressBar progress;
    int tmp = 0;
    Handler handler = new Handler();
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn1 = (Button)findViewById(R.id.button1);
        btn2 = (Button)findViewById(R.id.button2);
        progress = (ProgressBar)findViewById(R.id.progressBar1);
        
        btn1.setOnClickListener(new View.OnClickListener() {  //button控件事件监听
            @Override  
            public void onClick(View v) {  
            	handler.post(update);  //通过post函数,将Runnable对象加到消息队列中,根据消息结果更新progress
            }  
        });      
    }
    
    Runnable update = new Runnable() {  //创建子线程,专门处理耗时操作,然后在主线程更新progress。
        @Override  
        public void run() {  
            if (progress.getProgress() < 100) {  
                tmp += 10;  
                progress.setProgress(tmp);  
                progress.setSecondaryProgress(tmp + 10);  
                handler.postDelayed(update, 800); //延时0.8秒后,将Runnable对象重新加到消息队列中 
            } else {  
                handler.removeCallbacks(update); //从消息撤销Runnable对象
                progress.setVisibility(View.GONE);  //progress不可见
                tmp = 0;  
            }  
        }  
    };
}

二、第二种Handler通信办法是,通过子线程sendMessage函数,将消息发送到消息队列中。然后在主线程通过重写handlerMessage读取消息队列中Message,然后更新主线程。

      函数调用整个过程:

      通过post()将Runnable对象加到消息队列中,然后运行Runnable对象的run函数,通过sendMessage消息,run函数运行完后,在主线程调用handlerMessage函数,更新UI,如果UI更新完成,通过removeCallB
acks将Runnable对象从队列中撤销。

package com.test.ui;


import android.app.Activity;
import android.os.Handler;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class TestActivity extends Activity {
   
 

    private Button btn1;
    private ProgressBar progress;
    int tmp = 0;
    
    Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			progress.setProgress(msg.arg1);
			if(msg.arg1 == 100){
				handler.removeCallbacks(update);//更新完成了,将Runnable对象撤销
			      progress.setVisibility(View.GONE);
			      tmp = 0;
			}
			else
				handler.post(update);	//更新没完成,继续将要执行的线程放入到队列当中
		}
    };
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn1 = (Button)findViewById(R.id.button1);
        progress = (ProgressBar)findViewById(R.id.progressBar1);
        
        btn1.setOnClickListener(new View.OnClickListener(){//事件监听
        	 public void onClick(View v) {  
        	            //设置进度条为可见状态  
        	            progress.setVisibility(View.VISIBLE);  
        	            handler.post(update);  
        	  }  
        });
    }  
      
    
    Runnable update = new Runnable() {  //创建子线程,专门处理耗时操作,然后在主线程更新progress。

        public void run() { 
        		tmp =tmp + 10;
        	    Message msg = handler.obtainMessage(); 
        		msg.arg1 = tmp;
        		try{  
                    Thread.sleep(800); //让当前线程休眠800毫秒  
                }catch(InterruptedException ex){  
                    ex.printStackTrace();  
                }  
                handler.sendMessage(msg); //将消息发送到消息队列中,等待主线程调用handlerMessage函数处理(run运行完后处理)
        	
        	if(tmp == 100){
        		handler.removeCallbacks(update);//更新完成了,将Runnable对象撤销
        		tmp = 0;
        	}
        }  
    };
   
}

2、activity学习

3、service学习

4、Message学习

抱歉!评论已关闭.