利用handler实现定时器的两种方法

时间:2022-09-09 20:36:40
不多说,直接上 
Handler两种方式
第一种:
package com.example.demo;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class MainActivity extends Activity {
private EditText editText1;
TextView textView1;
Handler handler = new Handler();
Runnable runnable = new Runnable() {

@Override
public void run() {

handler.postDelayed(this, 2000);
Toast.makeText(MainActivity.this, "YQY_Editor", Toast.LENGTH_SHORT).show();
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
textView1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
handler.postDelayed(runnable, 2000);
}
});
}

}
第二种:
package com.example.demo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class MainActivity extends Activity {
private EditText editText1;
TextView textView1;
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:

Toast.makeText(getApplicationContext(), "aaa", 3000).show();
break;
default:
break;
}
};
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
textView1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
new Thread(new MyThread()).start();
}
});
}
public class MyThread implements Runnable{

@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(3000);
Message msg=new Message();

msg.what=1;
handler.sendMessage(msg);

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
}
}


}<pre name="code" class="java">调用handler.removeCallbacks(runnable);停止线程
package com.example.demo;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class MainActivity extends Activity {
private TextView editText1;
static Context context;
TextView textView1;
Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
editText1 = (TextView) findViewById(R.id.editText1);
context = this;
editText1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
handler.removeCallbacks(runnable);
}
});
textView1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
handler.post(runnable);
}
});
}

Runnable runnable = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(context, "aaa", 3000).show();
handler.postDelayed(runnable, 2000);
}
};

}