android in practice_Implementing jobs with AsyncTask(MyMovies project)
16lz
2021-01-26
AsyncTask offers an easy-to-use (but more limited).
You need to perform an asynchronous job that follows a pre-process/process/postprocesspattern, and are looking for a code template that allows you to report progress
to the user or otherwise update the UI in each step.
In the last technique, weretrieved movie thumbnail images by having a simple downloader helper method forka new thread that downloaded the image, and then passed it to a custom handlerobject that updated the image on the list view.
create theAsyncTask classDownloadTask:
public class DownloadTask extends AsyncTask<String,Void,Bitmap> { private int position; private ImageView imageView; private Drawable placeholder; public DownloadTask(int position,ImageView imageView){ this.position=position; this.imageView=imageView; Resources resources=imageView.getContext().getResources(); this.placeholder=resources.getDrawable(android.R.drawable.gallery_thumb); } //Called before task runs protected void onPreExecute(){ imageView.setImageDrawable(placeholder); } //task logicprotected Bitmap doInBackground(String... inputUrls) {try{URL url=new URL(inputUrls[0]);return BitmapFactory.decodeStream(url.openStream());}catch(Exception e){e.printStackTrace();return null;}} //called after task runsprotected void onPostExecute(Bitmap result){int forPosition=(Integer)imageView.getTag();if(forPosition==this.position){this.imageView.setImageBitmap(result);}}}worker arguments (String), worker progress (Void), and worker result (Bitmap).
trigger the task,modify the class MovieAdapter:
/** * Another perfectly valid approach would be to create a Movie model class. * get rid of the HashMap. *///The MovieAdapter keeps track of selected movies//Adapter separating data from its representation on the screenpublic class MovieAdapter extends ArrayAdapter<String> {//storing movie state data//this state is transientprivate HashMap<Integer,Boolean> movieCollection=new HashMap<Integer,Boolean>();private String[] movieIconUrls;//List of movie image URLs private ThreadPoolExecutor executor;//Controls thread pool public MovieAdapter(Context context) {super(context,R.layout.movie_item,android.R.id.text1,context.getResources().getStringArray(R.array.movies));// TODO Auto-generated constructor stub//Read image URLs into array movieIconUrls=context.getResources().getStringArray(R.array.movie_thumbs); }public void toggleMovie(int position){if(!isInCollection(position)){movieCollection.put(position, true);}else{movieCollection.put(position, false);}}public boolean isInCollection(int position){return movieCollection.get(position) == Boolean.TRUE;}@Override//returns the view needed for each item.//cache and reuse item views via convertView for performance reasonspublic View getView(int position,View convertView,ViewGroup parent){ View listItem=super.getView(position, convertView, parent); CheckedTextView checkMark=(CheckedTextView)listItem.findViewById(android.R.id.text1); checkMark.setChecked(isInCollection(position)); ImageView imageView =(ImageView)listItem.findViewById(R.id.movie_icon); imageView.setImageDrawable(null); imageView.setTag(position);//link position to image view String imageUrl=movieIconUrls[position]; new DownloadTask(position,imageView).execute(imageUrl); return listItem; }}first, the absence of any explicit interthread communication usinghandlers and messages, and second, a higher code quality achieved by having all coderelated to the download task in a single class.
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer