0

在我的 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>

谢谢!

4

3 回答 3

1

我建议您实现一项服务并将所有套接字连接代码移动/集中到该服务中。对于所有想要使用套接字连接的活动,绑定您的网络服务,onCreate然后在活动完成后finish,将其取消绑定到onDestory().

于 2012-06-24T21:07:38.590 回答
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对象。

于 2012-06-24T21:00:26.627 回答
0

您应该AndroidManifest.xml像这样定义自定义应用程序类的完全限定名称:

<application android:name="pfg.nao.naoControler.clientSocket" />

我相信这是ClassCastException这一行的原因:

((clientSocket)this.getApplication()).setSocket(c);

同样作为Application参考,最好使用单例。

于 2012-06-24T22:01:19.860 回答