Android Handler消息机制

来源:互联网 发布:淘宝优站管理 编辑:程序博客网 时间:2024/05/23 12:29

1.Handler的作用

(1).当应用程序启动时,Android首先会开启一个主线程(也就是UI线程) , 主线程是管理界面中的UI控件,而为了安全的考虑其他线程不能更新ui,这时候就需要android提供的Handler类来更新UI

(2) 安排消息或Runnable 在某个主线程中某个地方执行

(3)可以用于延时操作

 

2.Handler的使用

(1)更新UI

 

代码

以下代码实现了用Handler不停得增加TextView显示的直

package com.example.handlertest;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
   private static Buttonbtu1,btu2;
   private static TextViewtv;

   @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       if (savedInstanceState == null) {
           getFragmentManager().beginTransaction()
                   .add(R.id.container, new PlaceholderFragment())
                   .commit();
       }
   }

   @Override
    publicboolean onCreateOptionsMenu(Menu menu) {
       
       // Inflate the menu; this adds items to the action bar if it ispresent.
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
   }

   @Override
    publicboolean onOptionsItemSelected(MenuItem item) {
       // Handle action bar item clicks here. The action bar will
       // automatically handle clicks on the Home/Up button, so long
       // as you specify a parent activity in AndroidManifest.xml.
       int id = item.getItemId();
       if (id == R.id.action_settings) {
           return true;
       }
       return super.onOptionsItemSelected(item);
   }

   
    publicstatic class PlaceholderFragment extends Fragment {
    private static int i=0;
     Handler jishu = new Handler(){
      public voidhandleMessage(Message msg) {
       tv.setText(i+"");
       i++;

    //1秒后 再启动线程
        jishu.sendEmptyMessageDelayed(0,1000);
      }
     };
 
       public PlaceholderFragment() {
       }

       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroupcontainer,
               Bundle savedInstanceState) {
           View rootView = inflater.inflate(R.layout.fragment_main, container,false);
           btu1 = (Button)rootView.findViewById(R.id.button1);
           btu2 = (Button)rootView.findViewById(R.id.button2);
           tv= (TextView)rootView.findViewById(R.id.textView1);
           btu1.setOnClickListener(new OnClickListener() {
    
    @Override
    publicvoid onClick(View arg0) {
     tv.setText("");

//1秒后 启动线程
     jishu.sendEmptyMessageDelayed(0,1000);
    }
   });
           btu2.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0){

//销毁线程
              jishu.removeMessages(0);
            }
           });
           return rootView;
       }
   }

}

(2)延时操作

//延时1秒

new Handler().postDelayed(new Runnable() {
     @Override
     publicvoid run() {
     }
    },1000);

 

 


0 0
原创粉丝点击