一个Android异步调用的简单框架

gongzhiyao0 2012-07-25

1 异步需要执行的函数,完成需要做的事
定义一个接口,完成异步需要干的活:
public interface ThreadDoSomethingObj {

public ThreadExecuteResult DoSomething(Object input_obj);
}

2 异步执行完成后,执行结果的返回和执行数据的输出
定义一个输出结果类,输出result和Serializable 的对象:
public class ThreadExecuteResult {
int m_result;
Serializable m_obj;
public ThreadExecuteResult(int result, Serializable obj)
{
m_result = result;
m_obj = obj;
}

public void SetResult(int result)
{
m_result = result;
}

public int GetResult()
{
return m_result;
}
public void SetObj(Serializable obj)
{
m_obj = obj;
}

public Serializable GetObj()
{
return m_obj;
}
}

3 运行异步执行函数的线程管理类,可以在里面实现一个线程池的东东:
import Android.os.Handler;
import android.os.Looper;
import android.os.Message;

public class ThreadMgr extends Thread {
private ThreadDoSomethingObj m_thread_handler;
private Handler m_msg_handler;
private Object m_input_obj;

public ThreadMgr(Object input_obj,
ThreadDoSomethingObj thread_handler,
Handler callback_handle) {
m_thread_handler = thread_handler;
m_msg_handler = callback_handle;
m_input_obj = input_obj;
}

@Override
public void run() {
Looper.prepare();

ThreadExecuteResult result = m_thread_handler.DoSomething(m_input_obj);
Notify(result);

Looper.loop();
}

private void Notify(ThreadExecuteResult result)
{
Message msg = new Message();

msg.what = result.GetResult();

msg.getData().putSerializable("Object", result.GetObj());

m_msg_handler.sendMessage(msg);//
}
}

4 调用:


//调用函数
private void AsyncDemo() {
ThreadDoSomethingObj sth_obj = new ThreadDoSomethingObj() {

@Override
public ThreadExecuteResult DoSomething(Object input_obj) {
// TODO Auto-generated method stub
ThreadExecuteResult result = new ThreadExecuteResult(0,
new Person());

InputParmeter input = (InputParmeter) input_obj;
Map map = input.GetInputMap();

Log.v("Log", map.get("key"));

return result;
}
};
m_callback_handler = new MainHandler();

InputParmeter input_parm = new InputParmeter();

ThreadMgr thread_mgr = new ThreadMgr(input_parm, sth_obj,
m_callback_handler);
thread_mgr.run();
}


//输入Handler类
private class MainHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0: {
Toast.makeText(m_context, "result", Toast.LENGTH_SHORT).show();

Person result = (Person) msg.getData()
.getSerializable("Object");
Toast.makeText(m_context, "default, obj" + result.getName(),
Toast.LENGTH_SHORT).show();
}
break;
default: {

Person result = (Person) msg.getData()
.getSerializable("Object");
Toast.makeText(m_context, "default, obj" + result.getName(),
Toast.LENGTH_SHORT).show();
}
break;
}
}

}

//输入参数类
private class InputParmeter {
private Map m_map;

public InputParmeter() {
m_map = new HashMap();
}

public void AddInputParmeter(String name, String value) {
m_map.put(name, value);
}

public Map GetInputMap() {
return m_map;
}
}


//输出参数类
private class Person implements Serializable {
private static final long serialVersionUID = -7060210544600464481L;

private String name;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

相关推荐