android Handler浅谈
2010-05-31 14:21:00 来源:WEB开发网首先得明白 这是浅谈, 也就是说研究不深入,后续会继续跟踪.
我个人认为 android 线程间通信用 Handler +Message 方式.
进程间通信用AIDL.
呵呵,深知自己能力很差, 只是个人理解,希望明白的告诉我下.
为什么说 handler主要用于线呈间通信呢?
Java代码
package com.ray.handler;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.View;
public class BounceView extends View {
float x = 40;
public BounceView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
x+=10;
Paint mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.GREEN);
canvas.drawCircle(x, 40, 40, mPaint);
}
}
上面的代码就是实现了一个简单的Widget .
package com.ray.handler;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
public class TestHandler extends Activity {
protected static final int GUIUpdateIDENTIFIER = 0x101;
Thread myRefreshThread = null;
BounceView myBounceView = null;
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case TestHandler.GUIUpdateIDENTIFIER:
更多精彩
赞助商链接