在我的Android应用程序中我有一个套接字,但是当我旋转屏幕时,我必须保持套接字连接,以便将消息发送到服务器。所以我决定这样做:
package pfg.nao.naoControler;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Application;
public class clientSocket extends Application{
public Socket clientSocket;
@Override
public void onCreate()
{
super.onCreate();
}
public void setSocket(Socket socket) throws UnknownHostException, IOException{
clientSocket = socket;
}
public Socket getSocket() throws UnknownHostException, IOException{
return clientSocket;
}
}
在我的活动和线程中使用它的全局套接字。因为在某些情况下我必须能够打开和关闭连接。
例如,我尝试像这样访问clientSocket:
Socket c = new Socket(IP, puerto);
((clientSocket)this.getApplication()).setSocket(c);
但我有这个错误:
06-24 22:25:18.217: E/AndroidRuntime(25267): FATAL EXCEPTION: main
06-24 22:25:18.217: E/AndroidRuntime(25267): java.lang.ClassCastException: android.app.Application
06-24 22:25:18.217: E/AndroidRuntime(25267): at pfg.nao.naoControler.NaoControlerActivity.onOptionsItemSelected(NaoControlerActivity.java:160)
06-24 22:25:18.217: E/AndroidRuntime(25267): at android.app.Activity.onMenuItemSelected(Activity.java:2312)
06-24 22:25:18.217: E/AndroidRuntime(25267): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:769)
06-24 22:25:18.217: E/AndroidRuntime(25267): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
06-24 22:25:18.217: E/AndroidRuntime(25267): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
06-24 22:25:18.217: E/AndroidRuntime(25267): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
06-24 22:25:18.217: E/AndroidRuntime(25267): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
06-24 22:25:18.217: E/AndroidRuntime(25267): at android.view.View$PerformClick.run(View.java:9262)
06-24 22:25:18.217: E/AndroidRuntime(25267): at android.os.Handler.handleCallback(Handler.java:587)
06-24 22:25:18.217: E/AndroidRuntime(25267): at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 22:25:18.217: E/AndroidRuntime(25267): at android.os.Looper.loop(Looper.java:130)
06-24 22:25:18.217: E/AndroidRuntime(25267): at android.app.ActivityThread.main(ActivityThread.java:3744)
06-24 22:25:18.217: E/AndroidRuntime(25267): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 22:25:18.217: E/AndroidRuntime(25267): at java.lang.reflect.Method.invoke(Method.java:507)
06-24 22:25:18.217: E/AndroidRuntime(25267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-24 22:25:18.217: E/AndroidRuntime(25267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-24 22:25:18.217: E/AndroidRuntime(25267): at dalvik.system.NativeStart.main(Native Method)
这是我的android manyfest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pfg.nao.naoControler"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".NaoControlerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/settings" >
</activity>
</application>
<application
android:name=".clientSocket"
android:label="@string/cliente" >
</application>
</manifest>
谢谢!
答案 0 :(得分:1)
我建议您实现service并将所有套接字连接代码移动/集中到服务中。对于要使用套接字连接的所有活动,请在onCreate
中绑定您的网络服务,然后在finish
活动后,将其取消绑定到onDestory()
。
答案 1 :(得分:0)
我可能会做的(仅根据您提供的信息)是通过静态方法访问Socket
。这样您就不必担心Application
是否已初始化,或者您是否正在调用正确的Application
对象。
像这样的东西(为了避免混淆而重命名了几个东西......):
public class clientSocket extends Application {
private static Socket localSocket;
@Override
public void onCreate() {
super.onCreate();
}
public static void setSocket(Socket socket) throws UnknownHostException, IOException {
localSocket = socket;
}
public static Socket getSocket() throws UnknownHostException, IOException {
// Check here if it's null; if it is, then initialize it, or throw some exception, or something.
return localSocket;
}
}
然后,您可以像这样初始化它:
Socket c = new Socket(IP, puerto);
clientSocket.setSocket(c);
你可以这样称呼它:
Socket c = clientSocket.getSocket();
请注意:如果您在课程之间共享一个 Socket
,此方法仅才能正常工作。如果您有多个,则必须对它们进行排序,在您的类中拥有多个类(具有自己的Socket
s)或多个静态Socket
对象。
答案 2 :(得分:0)
您应该在AndroidManifest.xml
中定义自定义应用程序类的完全限定名称,如下所示:
<application android:name="pfg.nao.naoControler.clientSocket" />
我认为这是ClassCastException
这一行的原因:
((clientSocket)this.getApplication()).setSocket(c);
同样,当Application
引用停留时,最好使用单例。