startActivity(intent); 方法无法实现activity的跳转
public class ClientThread extends Thread
{
private Socket socket = null;
private ObjectOutputStream oos = null;
private ObjectInputStream ois = null;
private Context context = null;
private TransEntity trans = null;
public ClientThread()
{
this.socket = SysCtlVar.socket;
this.context = SysCtlVar.context;
this.trans = SysCtlVar.trans;
}
public void run()
{
if(SysCtlVar.FLAGS_ConnectionTimeOut==true)
{
SysCtlVar.client.startServer();
this.socket = SysCtlVar.socket;
}
initStream();
try
{
SysCtlVar.FlAGS_LoginSuccess = setLogin(context,trans);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
if(SysCtlVar.FlAGS_LoginSuccess==true)
{
synchronized(this)
{
try
{
this.wait();
new Jump2MainActivityThread().start();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
while(SysCtlVar.FlAGS_LoginSuccess)
{
}
}
public void initStream()
{
try
{
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
SysCtlVar.oos = oos;
SysCtlVar.ois = ois;
}
catch (IOException e)
{
e.printStackTrace();
}
}
@SuppressWarnings("static-access")
public boolean setLogin(Context context,TransEntity trans) throws ClassNotFoundException
{
boolean b=false;
try
{
//oos = new ObjectOutputStream(socket.getOutputStream());
System.out.println("--------Client----oos!=null :--------------"+(oos!=null));
//trans.setRequestTags(TransInfoTags.REQUEST_BUDDY_LIST); //发送一个要求返回在线好友的请求的Message
oos.writeObject(trans);
//ois = new ObjectInputStream(socket.getInputStream());
System.out.println("--------Client----ois!=null :--------------"+(ois!=null));
TransEntity intrans = (TransEntity)ois.readObject();
if(TransInfoTags.LOGIN_SUCCESS.equals(intrans.getResponseTags())&&(!b))
{
System.out.println("--------------Client LOGIN_SUCCESS -------------");
MainActivity.list = intrans.getList();
TransEntity outrans = new TransEntity();
outrans.setUserOperaType(SysCtlVar.NLSTR);//USER is online 避免同一用户重复登陆系统;
outrans.setResponseTags(SysCtlVar.NLSTR);
outrans.setRequestTags(UserOperaType.GET_AVATARS);
b = true;
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(outrans);
oos.flush();
System.out.println("-----------------trans.getRequestTags():--------------"+(outrans.getRequestTags()));
}
else if(TransInfoTags.LOGIN_FAIL.equals(intrans.getResponseTags()))
{
b = false;
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
return b;
}
}
class Jump2MainActivityThread extends Thread
{
private static Context firstActivity = null;
public Jump2MainActivityThread()
{
firstActivity = new FirstActivity();
}
public void run()
{
synchronized(SysCtlVar.clientThread)
{
Intent intent = new Intent();
intent.setClass(firstActivity,MainActivity.class);
SysCtlVar.context.startActivity(intent);
((Activity) firstActivity).finish();
SysCtlVar.clientThread.notify();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
把context传到子线程是不行的,必须在主线程才能startActivity。
线程同步建议用message。可以在主线程new一个handler,将handler传到子线程,子线程结束后postmessage到主线程handler里startActivity.