歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

一個Android異步調用的簡單框架

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;
}
}

Copyright © Linux教程網 All Rights Reserved