startActivity(intent); 方法无法实现activity的跳转

发布于 2021-11-25 09:45:07 字数 4049 浏览 756 评论 1

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一笑 2021-11-27 23:14:14

把context传到子线程是不行的,必须在主线程才能startActivity。
线程同步建议用message。可以在主线程new一个handler,将handler传到子线程,子线程结束后postmessage到主线程handler里startActivity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文