publishProgress
来源:互联网 发布:mac搜狗输入罗马数字 编辑:程序博客网 时间:2024/06/07 03:21
关于AsyncTask的publishProgress循环播放文字广告: activity_main.xml文件:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="example.com.handler.MainActivity" tools:showIn="@layout/activity_main"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn" android:text="start"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn1" android:text="stop"/></LinearLayout> 下面是MainActivity.java 文件package example.com.asynctaskdemo;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity { private TextView tv; private Button btn; private Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initListener(); } private void initView() { tv = (TextView) findViewById(R.id.tv); btn = (Button) findViewById(R.id.btn); btn1 = (Button) findViewById(R.id.btn1); } private class MyAsyncTask extends AsyncTask<String, Integer, Void> { private TextView textView; private String[] str = {"First advertisement", "Second advertisement"}; private int n = 0; private boolean stop = false; public MyAsyncTask(TextView textView) { this.textView = textView; } public void setStop(boolean stop){ this.stop=stop; } @Override protected Void doInBackground(String... params) { Log.d("Tag","doInBackgroud"); while (!stop) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } /** * n就是传入下面onProgressUpdate 的参数 * publishProgress是跨线程的, */ publishProgress(n); n++; Log.i("Tag","publishProgress"+n); Log.d("Tag", "publishProgress"); } return null; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); Log.d("Tag", "onProgressUpdate"); Log.i("Tag","n="+values[0]); textView.setText(n+str[values[0]%2]); // textView.setText("noThing"); } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); textView.setText("广告结束"); } } private MyAsyncTask myTask; private void initListener() { btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myTask = new MyAsyncTask(tv); myTask.execute(); } }); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myTask.setStop(true); } }); }}
0 0
- publishProgress
- AsyncTask 中的 publishProgress 方法
- AsyncTask中publishProgress的用法
- Android:异步任务AsyncTask、execute、publishProgress、onProgressUpdate
- Android:异步任务AsyncTask、execute、publishProgress、onProgressUpdate
- Android:异步任务AsyncTask、execute、publishProgress、onProgressUpdate
- 简单的自定义控件
- 23中设计模式之门面模式
- c++ 实现url的转码与解码 urlencode urldecode
- 部门团建—十渡
- android Service 之Bound Service
- publishProgress
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
- 数组长度的可扩张性写法的例子
- jdbc1
- img标签等比例缩放
- mybatis级联查询
- Android中的Selector的用法
- Parajumpers Herren knight the parajumpers
- 鼠标滑过图片切换函数(简化版)鼠标滑动效果