登录
首页 » Java » android 异步 执行 任务 例子 附讲解

android 异步 执行 任务 例子 附讲解

于 2013-07-05 发布
0 281
下载积分: 1 下载次数: 0

代码说明:

Rules::The AsyncTask instance must be created in UI thread. .execute must be invoked on the UI thread.Never call  objMyTask.onPreExecute(), objMyTask.doInBackground(), objMyTask.onProgressUpdate(),  objMyTask.onPostExecute manually.The AsyncTask can be executed only once (an exception will be thrown if a second execution is attempted.)AsyncTask have Four Main Method... onPreExecute()  doInBackground() onProgressUpdate() onPostExecute()  onPreExecute-This method is called first when you start AsyncTask using objAsync.execute().And mostly this method is use for initializing dialog(ProgressDialog,CustomDialog) and showing. doInBackground-The main purpose of AsyncTask is accomplished by this method.Any non-UI thread process is running in this method.Such as Rss Feed Reader,Image and video Uploading and Downloading.You cant handle your View in this method.Because this method is non-UI thread.While any background process is running if you want to handle UI therea are  onProgressUpdate method. after completion of process this method send result to OnPostExecute. onProgressUpdate-While backgrounding task is running ,you can handle your UI using this method .Such as status of downloading or uploading task.and this method is called from  doInBackground.Using publishProgress() you can call onProgressUpdate method to update UI while process is running. onPostExecute -This method is called after the background computation finishes.The result of background process in passed in this method as parameters.And now you can dismiss progress dialog ,to indicate that background task is completed. You can cancel AsyncTask using objAsyncTask.cancel().then you just check in doInBackground, if (isCancelled()) { break; } else {        //continue... } See this Image For more Clear.

下载说明:请别用迅雷下载,失败请重下,重下不扣分!

发表评论

0 个回复

  • android uvcCamera
    【实例简介】usb camera的连接使用
    2021-07-17 00:32:04下载
    积分:1
  • 网络下载图片01
    网络下载图片01
    2015-04-22下载
    积分:1
  • 批量替换 android反编译之后 R.java文件中的数字(id对应的值) 实例源码下载
    好多人反编译都遇到 反编译出来的 R.id.... 这些都变成了数字,这个程序就是批量替换这些数字为可读性更好的id,支持 UTF-8  GBK gb2312 ,没乱码,亲测通过
    2014-05-26下载
    积分:1
  • android 课表视图 例子源码
    android 课表视图 例子源码
    2014-10-20下载
    积分:1
  • android 实现类似新闻app的导航栏 例子源码下载
    android 实现类似新闻app的导航栏 例子源码下载
    2015-04-22下载
    积分:1
  • Vitamio 视频播放器 的使用
    Vitamio 视频播放器 的使用
    2014-05-26下载
    积分:1
  • android 自定义充值控件 实例源码
    android 自定义充值控件 实例源码
    2014-06-03下载
    积分:1
  • android 自定义Switch控件 例子源码下载(兼容低版本)
    兼容android低版本
    2015-04-15下载
    积分:1
  • android使用JSON进行网络数据交换(服务端、客户端)的实现
    android使用JSON进行网络数据交换(服务端、客户端)的实现
    2015-04-17下载
    积分:1
  • android 播放网络MP3 音乐播放
    简单的基本功能,实测可行。 核心代码:package com.sharpandroid.music.activity;import java.io.IOException;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ImageButton;import android.widget.SeekBar;import android.widget.TextView;import com.sharpandroid.music.R;import com.sharpandroid.music.StreamingMediaPlayer;public class MediaPlayer extends Activity { private Button streamButton; private ImageButton playButton; private boolean isPlaying; private TextView playTime; private StreamingMediaPlayer audioStreamer; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); initControls(); } private void initControls() { playTime=(TextView) findViewById(R.id.playTime); streamButton = (Button) findViewById(R.id.button_stream); streamButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { startStreamingAudio(); }}); playButton = (ImageButton) findViewById(R.id.button_play); playButton.setEnabled(false); playButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { if (audioStreamer.getMediaPlayer().isPlaying()) { audioStreamer.getMediaPlayer().pause(); playButton.setImageResource(R.drawable.button_play); } else { audioStreamer.getMediaPlayer().start(); audioStreamer.startPlayProgressUpdater(); playButton.setImageResource(R.drawable.button_pause); } isPlaying = !isPlaying; }}); } private void startStreamingAudio() { try { final SeekBar progressBar = (SeekBar) findViewById(R.id.progress_bar); if ( audioStreamer != null) { audioStreamer.interrupt(); } audioStreamer = new StreamingMediaPlayer(this, playButton, streamButton, progressBar,playTime); audioStreamer.startStreaming("http://192.168.64.1/xa.mp3",5208, 216); streamButton.setEnabled(false); } catch (IOException e) { Log.e(getClass().getName(), "读取音乐出错!", e); } }}
    2014-05-26下载
    积分:1
  • 696516资源总数
  • 106914会员总数
  • 0今日下载