android studio 定时器操作 实现定时执行相关任务
package ipget.wenzheng.studio.ipget; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class VolleyDemoActivity extends AppCompatActivity implements View.OnClickListener { private Button button; //初始化按钮控件 private Button button2; //初始化按钮控件 private TextView TextView1; //初始化文本控件 private TextView textView2; //初始化文本控件 private EditText ip; //初始化编辑框 private TextView yanwu; //初始化文本控件 private Button stop; Timer timer = new Timer(); //定义全局变量 long delay = 2000; long intevalPeriod = 5* 1000; TimerTask task = new TimerTask() { @Override public void run() { Message message = new Message(); message.what = 1; handler.sendMessage(message); }}; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub // 要做的事情 super.handleMessage(msg); //Toast.makeText(VolleyDemoActivity.this,"ceshi",Toast.LENGTH_SHORT).show(); test(); } }; @Override protected void onCreate(Bundle savedInstanceState) { //直接运行的界面 super.onCreate(savedInstanceState); setContentView(R.layout.content_main); //进入主界面 initView(); //界面初始化 initListener(); //初始化监听 } private void initListener() { button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //test(); timer.scheduleAtFixedRate(task, delay,intevalPeriod); //开启定时器 } }); //团队简介按钮的单机事件,toast弹出名字 button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(VolleyDemoActivity.this,"团队成员:金玲,郭殊辰,李学倩",Toast.LENGTH_SHORT).show(); } }); stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { timer.cancel(); task.cancel(); } }); } /** * 初始化控件 * 获取控件的ID */ private void initView() { button2 = (Button) findViewById(R.id.button2); ip = (EditText) findViewById(R.id.ip); button = (Button)findViewById(R.id.button); TextView1 = (TextView) findViewById(R.id.textView1); textView2 = (TextView) findViewById(R.id.textView2); yanwu = (TextView) findViewById(R.id.yanwu); stop = (Button) findViewById(R.id.stop); } private void test() { Toast.makeText(VolleyDemoActivity.this,"正在刷新数据。。。",Toast.LENGTH_SHORT).show(); //创建一个请求队列 RequestQueue requestQueue = Volley.newRequestQueue(VolleyDemoActivity.this); //创建一个网络请求 String url_GET = ip.getText().toString() ; //第一个参数为url 第二个参数为 监听回调,新建即可 StringRequest stringRequest = new StringRequest(url_GET, new Response.Listener<String>() { //正确接收数据的回调 @Override public void onResponse(String response) { // 分解数据 //将从网页获取到的数据进行分解,倒序查找的方法进行分解 //取出温度值 String tval = response.substring(response.length()-19,response.length()-14); //取出湿度值 String hval = response.substring(response.length()-13,response.length()-8); //取出烟雾值 String yval = response.substring(response.length()-26,response.length()-20); //将数据存储到相应控件中 TextView1.setText(tval); textView2.setText(hval); yanwu.setText(yval); } }, new Response.ErrorListener() { //错误的监听回调 @Override public void onErrorResponse(VolleyError error) { textView2.setText("加载失败"+error); } }); //将创建的请求添加到队列中 requestQueue.add(stringRequest); } @Override protected void onStop() { super.onStop(); //当Activity停止运行后,取消Activity的所有网络请求 MyApplication.getRequestQueue().cancelAll(VolleyRequestDemo.VOLLEY_TAG); Log.i("### onStop", "cancel all:tag=" + VolleyRequestDemo.VOLLEY_TAG); } @Override public void onClick(View v) { //另外一种单击事件的方法,可以学习一下在下面判断ID即可 } }
@青山不移,文笔不息。学习,坚持,梦想青春!