[安卓开发基础] 子线程实现 Handler

1.步骤:[安卓开发基础] 子线程实现 Handler_第1张图片 

2.子线程: 

package com.viewflipper;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
/**
 * Created by zengjx on 2019/1/20.
 */
public class HandlerThread    extends   Thread {
   private Handler handler;
    private Context  mContext;
   private   Message   message;
    @Override
    public void run() {
        super.run();
        Looper.prepare();//初始化Looper对象

        handler =new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
               if(msg.what==1){
                  Toast.makeText(mContext,"子线程Handler",Toast.LENGTH_SHORT).show();//消息提醒
                   Log.i("Handler","9999999999999");
               }
                return false;
            }
        });
       //创建Handler 发送消息
        message =Message.obtain();
        message.what=1;
        handler.sendMessage(message);
        Looper.loop();//启动Looper
}
   public   void  SetContext(Context   context){
        mContext=context;
   }
}

 MainActivity:

package com.viewflipper;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
import com.zengjx.androidbaseproject.R;
public class MessageActivity extends AppCompatActivity {
    final    private    int  MSG=1;//消息代码
    private Context  context;
   private ViewFlipper    viewFlipper;
   private Animation []  animations=new  Animation[2];
   private   Message   message;
   private  int[] images=new   int[]{R.drawable.taoboimg1,R.drawable.taoboimg2,
           R.drawable.taoboimg3,R.drawable.taoboimg4,
           R.drawable.taoboimg5,R.drawable.taoboimg6,
           R.drawable.taoboimg7,R.drawable.taoboimg8,
   };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message);
        context=getApplicationContext();
        viewFlipper=findViewById(R.id.messsage_viewflipper);
        animations[0]= AnimationUtils.loadAnimation(this,R.anim.slide_in_left);
        animations[1]= AnimationUtils.loadAnimation(this,R.anim.slide_out_right);
        viewFlipper.setInAnimation(animations[0]);
        viewFlipper.setOutAnimation(animations[1]);

        for(int  i=0;i<8;i++){

            ImageView   imageView =new ImageView(this);
            imageView.setImageResource(images[i]);
            viewFlipper.addView(imageView);//

        }
        message= Message.obtain();
        message.what=MSG;
        mHandler.sendMessage(message);
    }

 private Handler mHandler = new Handler(new Handler.Callback() {

@Override

public boolean handleMessage(Message msg) {
switch (msg.what){

    case 1:
        viewFlipper.showPrevious();//显示下一张
        HandlerThread   handlerThread=       new  HandlerThread();//创建子线程
        handlerThread.SetContext(context);
        handlerThread.start();

     break;
}
message=mHandler.obtainMessage(MSG);
mHandler.sendMessageDelayed(message,3000);
return false;

 }

});
}

你可能感兴趣的:(安卓基础)