/*
* 处理后台线程
*/
public void doBackGround(){
if(!CurInfo.Info.ai_logined){
//Toast.makeText( this, "正在获取数据...", Toast.LENGTH_SHORT ).show();
/*
// 这里这个参数指定的是线程的名字
HandlerThread handlerThread = new HandlerThread("MyHandler");
// 在使用HandlerThread的getLooper()方法之前,必须先调用该类的start();
handlerThread.start();// 启动这个线程
//handlerThread.getLooper();
MyHandler myHandler = new MyHandler(handlerThread.getLooper());
*/
/*
// 这里这个obtainMessage()方法返回
Message msg = myHandler.obtainMessage();
// 将msg发送到目标对象,所谓的目标对象,就是生成该msg对象的handler对象
Bundle b = new Bundle();
b.putInt("age", 20);
b.putString("name", "Jhon");
msg.setData(b);
msg.sendToTarget();
*/
new MyAsyncTask().doInBackground();
}
}
/*
* 线程处理
*/
class MyHandler extends Handler {
public MyHandler() {
}
public MyHandler(Looper looper) {
super(looper);
// 这里处理任务
String result = AIData.aiInfo.loginHouse("999", "888");
if(result != "error"){
AIData.aiInfo.GetDeviceList(3);
CurInfo.Info.ai_SeeionId = result;
CurInfo.Info.ai_logined = true;
}
}
@Override
public void handleMessage(Message msg) {
// 这里我们只做了简单的输出
/*
Bundle b = msg.getData();
int age = b.getInt("age");
String name = b.getString("name");
System.out.println("age is " + age + ", name is" + name);
System.out.println("Handler--->" + Thread.currentThread().getId());
System.out.println("handlerMessage");
*/
}
}
/*
* 线程处理
*/
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
//耗时操作,后台线程处理
try {
//Thread.sleep(4*1000);
String result = AIData.aiInfo.loginHouse("999", "888");
if(result != "error"){
AIData.aiInfo.GetDeviceList(3);
CurInfo.Info.ai_SeeionId = result;
CurInfo.Info.ai_logined = true;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
//作UI线程的修改。
//progressDialog.dismiss();
super.onPostExecute(result);
}
}