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

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

于 2013-07-05 发布
0 309
下载积分: 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 个回复

  • MySQL Workbench 6.3 CE 汉化文件v0.3
    鉴于之前的汉化文件使用后报错,遂用两个小时写了个自动化脚本来汉化文件,亲测可用。时间仓促,若有不足,请在下方教程链接中提意见MySQL Workbench汉化文件,可根据教程完成汉化教程链接:https://blog.csdn.net/weixin_40423737/article/details/80193680
    2020-05-26下载
    积分:1
  • android 转盘抽奖程序源码 很容易扩展
    android 转盘抽奖程序源码 很容易扩展
    2014-01-02下载
    积分:1
  • android camera 摄像头拍照例子源码
    android camera 摄像头拍照例子源码
    2014-11-10下载
    积分:1
  • android 项目 五子棋游戏 项目源码
    android 项目 五子棋游戏 项目源码
    2013-10-28下载
    积分:1
  • android应用集成在线客服功能(可IM实时聊天) 例子源码下载
    android应用集成在线客服功能(可IM实时聊天) 例子源码下载
    2015-04-26下载
    积分:1
  • android java 通用代码,关于用properties存储打印Log 例子
    android java 通用代码,关于用properties存储打印Log 例子
    2014-09-17下载
    积分:1
  • android 发送接收短信例子源码
    android 发送接收短信例子源码
    2014-12-23下载
    积分:1
  • android 静态壁纸实例源码下载
    一款还不错的静态壁纸代码 核心代码:package com.example.wallpaperchange;import android.os.Bundle;import android.app.Activity;import android.app.AlarmManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast; public class MainActivity extends Activity { AlarmManager aManager; Button start,stop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button)findViewById(R.id.start); stop = (Button)findViewById(R.id.stop); aManager = (AlarmManager)getSystemService(Service.ALARM_SERVICE); //指定ChangeService组件 Intent intent = new Intent(MainActivity.this,ChangeService.class); //创建PendingIntent对象 final PendingIntent pi = PendingIntent.getService( MainActivity.this, 0, intent, 0); start.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub aManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 5000, pi); start.setEnabled(false); stop.setEnabled(true); Toast.makeText(MainActivity.this, "壁纸定时更换启动成功", 5000).show(); } }); stop.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub start.setEnabled(true); stop.setEnabled(false); aManager.cancel(pi); } }); } }
    2014-06-03下载
    积分:1
  • android 提醒小闹钟 例子源码
    android 提醒小闹钟 例子源码
    2014-10-17下载
    积分:1
  • 实现android相册网格和gallery功能 实例源码下载
    实现android相册网格和gallery功能 实例源码下载
    2015-03-17下载
    积分:1
  • 696516资源总数
  • 106914会员总数
  • 0今日下载