in my android application I have a socket, but when I rotate the screen, I have to keep the socket connected, to send messages to a server. So I decided to do this:
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;
}
}
A global socket to use it in my activities and threads. Because I have to be able to open and close the connection in some cases.
For example, I try to access to clientSocket like this:
Socket c = new Socket(IP, puerto);
((clientSocket)this.getApplication()).setSocket(c);
But I have this error:
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)
Here it is my 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>
Thanks!
What I’d probably do (just based on the info you’ve given) is make the
Socketaccessible via static method. This way you don’t have to worry about whether or not theApplicationis initialized, or if you’re calling the rightApplicationobject.Something like this (renamed a couple things to avoid confusion…):
Then, you can initialize it like this:
And you can call it like this:
Keep in mind: this method will only work if you have just one
Socketthat you are sharing between classes. If you have multiple, you would have to array them, have multiple classes (with their ownSockets), or multiple staticSocketobjects in your class.