android回调机制例子让你马上明白

在android开发的时候,我总想在MainActivity类以外的类里把数据传回主线程里在更新UI,但是一直想不到怎么做,今天看了《android高级开发实例》让我学会了 回调机制 ,解决了一直困扰我的问题。

好了废话到此结束,上正片!!


具体思路:

1.在另类里获取完数据后调用一个抽象方法;

2.在主线程里创建一个匿名内部类继承另类,并在匿名内部类里重写抽象方法,方法里完成只能在主线程里完成的工作;

<!-- 另类里获取完数据后,调用抽象方法,实际上是调用了匿名内部类(子类)重写的方法 -->


GetJsonl类里通过HTTP协议访问网络(具体用到HttpClient接口),把获得的数据回调到主线程里;

1.这里写了一个   getresponse  的抽象方法

2.在完全获取数据后调用 getresponse抽象方法


public abstract class GetJson {

	public void connect() {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					HttpClient httpClient = new DefaultHttpClient();
					HttpGet httpGet = new HttpGet(
							"http://www.weather.com.cn/data/cityinfo/101190404.html");
					HttpResponse httpResponse = httpClient.execute(httpGet);
					if (httpResponse.getStatusLine().getStatusCode() == 200) {
						HttpEntity httpEntity = httpResponse.getEntity();
						String response = EntityUtils.toString(httpEntity,
								"UTF-8");
						getresponse(response.toString());
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}).start();
	}

	public abstract void getresponse(String response);

}

MainActivity类里:

1.主线程里 创建一个继承GetJson的匿名内部类,并重写getresponse抽象方法

2.重写的getresponse方法里调用hanler.sendMessage更新UI


public class Main_Activity extends Activity {
	Button button;
	TextView textview;

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			textview.setText(msg.toString());
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		button = (Button) findViewById(R.id.button);
		textview = (TextView) findViewById(R.id.textview);
                                GetJson getjson = new GetJson() {

					@Override
					public void getresponse(String response) {
						// TODO Auto-generated method stub
						Message msg = new Message();
						msg.obj = response;
						handler.sendMessage(msg);
					}
				};
                                button.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View arg0) {
                                          // TODO Auto-generated method stub
                                           getjson.connect();
                                            } 
                                 });
           }
}


main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


记得声明权限

 <uses-permission android:name="android.permission.INTERNET" />

最后打印结果:



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值