在子线程实例化的时候,初始化一下全局变量
就是用LoadCall,LoadContacts,LoadSms这三个函数
但是感觉这三个函数并没有被调用
为什么?
部分代码如下package com.example.guarder;public class MainActivity extends Activity { private TabHost m_TabHost;
static final int REQUEST_CODE_BLOCK = 0;
static final int REQUEST_CODE_BLACK = 1;
static final int REQUEST_CODE_WHITE = 2;
static public String blockModel;
private TextView tv01;
private TextView tv02;
private static final int SETMAIN = 1;
private static final int SETCHILD = 2;
private Handler mainHandler,childHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mainHandler = new Handler()
{
public void handleMessage(Message msg)
{
switch(msg.what)
{
case SETMAIN:
break;
}
}
};
new Thread(new ChildThread(),"Child Thread").start();
this.startLoading();
}
public void startLoading()
{
if(MainActivity.this.childHandler != null)
{
Message childMsg = MainActivity.this.childHandler.obtainMessage();
childMsg.what = SETCHILD;
MainActivity.this.childHandler.sendMessage(childMsg);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
MainActivity.this.childHandler.getLooper().quit();
} class ChildThread implements Runnable
{
@Override
public void run()
{
Looper.prepare();
MainActivity.this.childHandler = new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what) {
case SETCHILD: {
Message toMain = MainActivity.this.mainHandler
.obtainMessage();
toMain.what = SETMAIN;
MainActivity.this.mainHandler.sendMessage(toMain);
Context context = getBaseContext();
LoadCall loadcall = new LoadCall(context); // 通话记录初始化
LoadContacts loadcontacts = new LoadContacts(context); // 联系人记录初始化
LoadSms loadsms = new LoadSms(context); // 短信记录初始化
break;
}
}
}
};
Looper.loop();
}
}}
就是用LoadCall,LoadContacts,LoadSms这三个函数
但是感觉这三个函数并没有被调用
为什么?
部分代码如下package com.example.guarder;public class MainActivity extends Activity { private TabHost m_TabHost;
static final int REQUEST_CODE_BLOCK = 0;
static final int REQUEST_CODE_BLACK = 1;
static final int REQUEST_CODE_WHITE = 2;
static public String blockModel;
private TextView tv01;
private TextView tv02;
private static final int SETMAIN = 1;
private static final int SETCHILD = 2;
private Handler mainHandler,childHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mainHandler = new Handler()
{
public void handleMessage(Message msg)
{
switch(msg.what)
{
case SETMAIN:
break;
}
}
};
new Thread(new ChildThread(),"Child Thread").start();
this.startLoading();
}
public void startLoading()
{
if(MainActivity.this.childHandler != null)
{
Message childMsg = MainActivity.this.childHandler.obtainMessage();
childMsg.what = SETCHILD;
MainActivity.this.childHandler.sendMessage(childMsg);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
MainActivity.this.childHandler.getLooper().quit();
} class ChildThread implements Runnable
{
@Override
public void run()
{
Looper.prepare();
MainActivity.this.childHandler = new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what) {
case SETCHILD: {
Message toMain = MainActivity.this.mainHandler
.obtainMessage();
toMain.what = SETMAIN;
MainActivity.this.mainHandler.sendMessage(toMain);
Context context = getBaseContext();
LoadCall loadcall = new LoadCall(context); // 通话记录初始化
LoadContacts loadcontacts = new LoadContacts(context); // 联系人记录初始化
LoadSms loadsms = new LoadSms(context); // 短信记录初始化
break;
}
}
}
};
Looper.loop();
}
}}
这是异步的,
public void startLoading()
这个里面调用了childHandler,但可能这时childHandler还有被始化.....即使ChildThread是在strtLoading之前调用.以上是猜测
我增加了一些Log...
但只是记录到了Log.d("A", "create");这个....
这是什么问题?
public class MainActivity extends Activity {
private static final int SETMAIN = 1;
private static final int SETCHILD = 2;
private Handler mainHandler,childHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mainHandler = new Handler()
{
public void handleMessage(Message msg)
{
switch(msg.what)
{
case SETMAIN:
Log.d("A", "mainhandler");
break;
}
}
};
new Thread(new ChildThread(),"Child Thread").start();
this.startLoading();
Log.d("A", "create");
}
public void startLoading()
{
if(MainActivity.this.childHandler != null)
{
Message childMsg = MainActivity.this.childHandler.obtainMessage();
childMsg.what = SETCHILD;
MainActivity.this.childHandler.sendMessage(childMsg);
Log.d("A", "loading");
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
MainActivity.this.childHandler.getLooper().quit();
} class ChildThread implements Runnable
{
@Override
public void run()
{
Looper.prepare();
MainActivity.this.childHandler = new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what) {
case SETCHILD: {
Message toMain = MainActivity.this.mainHandler
.obtainMessage();
toMain.what = SETMAIN;
MainActivity.this.mainHandler.sendMessage(toMain);
Context context = getBaseContext();
LoadCall loadcall = new LoadCall(context); // 通话记录初始化
LoadContacts loadcontacts = new LoadContacts(context); // 联系人记录初始化
LoadSms loadsms = new LoadSms(context); // 短信记录初始化
Log.d("A", "childhandler");
break;
}
}
}
};
Looper.loop();
}
}}
代码如上,只能看到Log.d("A", "create");这个执行了...
然后加载完了用mainHandler传出消息就好了 childHandler 完全多余 你将childHandler 的创建去掉 直接在子线程处理需要的操作
class ChildThread implements Runnable
{
@Override
public void run()
{
Context context = getBaseContext();
LoadCall loadcall = new LoadCall(context); // 通话记录初始化
LoadContacts loadcontacts = new LoadContacts(context); // 联系人记录初始化
LoadSms loadsms = new LoadSms(context); // 短信记录初始化
Log.d("A", "childhandler");
Message toMain = MainActivity.this.mainHandler
.obtainMessage();
toMain.what = SETMAIN;
MainActivity.this.mainHandler.sendMessage(toMain);
}
}