Android logoff, handler, message, and messagequeue applications

Source: Internet
Author: User

 

Introduction

The previous article introduced Android thread interaction content such as handler, message, and messagequeue, and Android's understanding of logoff, handler, message, and messagequeue. Next, we will start to learn how to use and apply it to a program.

Instance

Use listview as the environment for asynchronous image download.

1. Handle + runnable

The implementation logic is as follows:

1: Enable a thread in the UI thread to download images.

2: After the image is downloaded, a message is sent to notify the UI thread.

3: After the UI thread obtains the message, it updates the UI.

package com.example.handlerloadiage;import java.io.IOException;import java.net.URL;import android.app.Activity;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;public class Handler_Runnable_Mode extends Activity {private ListView listview;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_handlerimageloader);listview = (ListView) findViewById(R.id.listview);listview.setAdapter(new MyAdapter());}private class MyAdapter extends BaseAdapter {public MyAdapter() {}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn 100;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) {convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item, null);}final ImageView image = (ImageView) convertView.findViewById(R.id.imageview);final String imageURL = "http://avatar.csdn.net/D/1/4/3_wangjinyu501.jpg";Handler handler = new Handler();handler.post(new Runnable() {public void run() {Drawable drawable = null;try {drawable = Drawable.createFromStream(new URL(imageURL).openStream(), "image.jpg");} catch (IOException e) {Log.d("test", e.getMessage());}if (drawable == null) {Log.d("test", "null drawable");} else {Log.d("test", "not null drawable");}if (drawable == null) {image.setImageResource(R.drawable.ic_launcher);} else {image.setImageDrawable(drawable);}}});return convertView;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}

The effect is as follows:

ANR still occurs during the fast sliding process.

This is because the handler. Post (New runnable () method does not enable a new thread. It is still in the main UI thread, which leads to ANR.

 

2. Handle + runnable + message

Implementation ideas:

Implementation Code:

 

package com.example.handlerloadiage;import java.io.IOException;import java.net.URL;import android.app.Activity;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;public class Handler_Runnable_Mode extends Activity {private ListView listview;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_handlerimageloader);listview = (ListView) findViewById(R.id.listview);listview.setAdapter(new MyAdapter());}private class MyAdapter extends BaseAdapter {public MyAdapter() {}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn 100;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) {convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item, null);}final ImageView image = (ImageView) convertView.findViewById(R.id.imageview);final String imageURL = "http://avatar.csdn.net/D/1/4/3_wangjinyu501.jpg";final Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);Drawable d=(Drawable) msg.obj;if (d == null) {image.setImageResource(R.drawable.ic_launcher);} else {image.setImageDrawable(d);}}};handler.post(new Runnable() {public void run() {Drawable drawable = null;try {drawable = Drawable.createFromStream(new URL(imageURL).openStream(), "image.jpg");Message message=handler.obtainMessage();message.obj=drawable;handler.sendMessage(message);} catch (IOException e) {Log.d("test", e.getMessage());}if (drawable == null) {Log.d("test", "null drawable");} else {Log.d("test", "not null drawable");}}});return convertView;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}

The ANR phenomenon also occurs, for the same reason as before.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.