关闭

android多线程Handler

标签: android多线程
279人阅读 评论(0) 收藏 举报
分类:
package com.example.thread_1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private Handler handler;
	private Button button1;
	private String result1 = null, result2 = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) findViewById(R.id.button1);
		//final RunClass runClass = new RunClass();
		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				Run1();// //桐城市
				Run2();// /舒城

				//runClass.RunThread3();// /巢湖
				//runClass.RunThread4();// /庐江
			}
		});

		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO 自动生成的方法存根
				// super.handleMessage(msg);
				switch (msg.what) {
				case 1:
					if (result1 != null) {
						Log.i("result1", result1);
					}

					break;
				case 2:
					if (result2 != null) {
						Log.i("result2", result2);
					}
					break;

				default:
					Log.i("错误", "错误");
					break;
				}

			}

		};

	}// /onCreate

	public void Run1() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO 自动生成的方法存根
				send1();
				Message m = new Message();
				m.what = 1;
				handler.sendMessage(m);

			}

		}).start();

	}

	// /
	private void send1() {
		// TODO 自动生成的方法存根 ////桐城市
		String target1 = "http://api.map.baidu.com/geocoder?output=json&location=31,117&key=Gl....";
		URL url1;

		try {
			url1 = new URL(target1);

			HttpURLConnection conn1 = (HttpURLConnection) url1.openConnection();

			BufferedReader buffer1 = new BufferedReader(new InputStreamReader(
					conn1.getInputStream()));
			String inputLine1 = null;
			while ((inputLine1 = buffer1.readLine()) != null) {
				result1 += inputLine1 + "\n";
			}
		} catch (MalformedURLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

	}// /send

	public void Run2() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO 自动生成的方法存根 ////舒城
				String target2 = "http://api.map.baidu.com/geocoder?output=json&location=31.3,117&key=Gl.....";
				URL url2;

				try {
					url2 = new URL(target2);

					HttpURLConnection conn2 = (HttpURLConnection) url2
							.openConnection();

					BufferedReader buffer2 = new BufferedReader(
							new InputStreamReader(conn2.getInputStream()));
					String inputLine2 = null;
					while ((inputLine2 = buffer2.readLine()) != null) {
						result2 += inputLine2 + "\n";
					}
					//Log.i("result2", result2);
					//
					// in_2.close();
					// conn_2.disconnect();
					Message m = new Message();
					m.what = 2;
					handler.sendMessage(m);
				} catch (MalformedURLException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}

		}).start();

	}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:157348次
    • 积分:4783
    • 等级:
    • 排名:第6003名
    • 原创:314篇
    • 转载:102篇
    • 译文:0篇
    • 评论:26条
    文章分类
    最新评论