我的问题可能会在同一时间变得简单和尴尬,但我有点卡住了。Android:从线程调用活动
我有一个Main.java类,它扩展了Activity。
这个类里面,我做了以下内容:
ringerServer = new Thread(new RingerServer());
ringerServer.start();
我想要做的就是有RingerServer线程运行的汽车无。
在该线程中,我侦听TCP连接。如果我得到一个,我开始另一个类,它发送和接收UDP数据包。
public class RingerServer implements Runnable {
public static final int SERVERPORT = 4445; // Default port to connect to
@Override
public void run() {
try {
// Create a socket for handling incoming requests
ServerSocket server = new ServerSocket(SERVERPORT);
while (!VoIPCall.onCall) {
// Wait for an incoming connection
Socket clientSocket = server.accept();
// TODO: Display a message for the user to accept or decline
// For now, automatically accept the call
Intent myIntent = new Intent(null, VoIPCall.class);
// Put the IP as a parameter
myIntent.putExtra("inetAddress", clientSocket.getInetAddress());
startActivity(myIntent);
}
} catch (IOException e) {
Log.e("TCP", "S: Error", e);
}
}
}
我的问题已经做的线:
Intent myIntent = new Intent(null, VoIPCall.class);
myIntent.putExtra("inetAddress", clientSocket.getInetAddress());
startActivity(myIntent);
这些线将做工精细的活动内,但它并没有抱怨,因为它是一个线程,它不知道Activity类,因为它没有扩展它,但实现了Runnable。
我不知道如何让我的程序继续运行RingerServer,但主线程转到VoIPCall类。请有任何想法吗?
我真的很感谢你的帮助。
非常感谢你,
Jary
您代码中的一个问题是您将null作为上下文传递给您的意图。你需要传递一个实际的Context对象来使这个调用工作。如果你希望你的Thread成为一个单独的类,那么你可能应该创建一个构造函数,它将Context作为它的参数。您可以将实例化线程的Activity或Service实例传入构造函数中。 – elevine 2010-11-28 16:00:52