Carrying SmartFoxServer connection across Android Activities -


I am using SmartFoxServer for a multiplayer game. When it loads in the main chat room, I have to work properly in my app, you make new game rooms and when you click on one, it launches a new activity and enters that room. It appears that after this I have to connect to the server. Then I already get the log error.

How can I create a Smartphone customer in all my activities?

You can override the application class and your server can refer to a static public variable which That will be available from any activity using (your application) getApplication (). YourStaticServerReference .

To override your application class, follow these steps :

  • Create a class expanded application, let's call it com.yourpackage.Your application

      Package com. Yurpackage; Import Android.app; Application; Public Class YourApplication application {Public static SmartFoxServer smartFoxServer; 
      @Orride Creates on Public Zero { SmartFoxServer = initSmartFoxServer (); // I do not know the Smartphone server's API so that let's apply this application anywhere in the classroom. // now the smartfoxserver area is like a global variable that is appearing in all your activities ((Your app) getApplication ()). SmartFoxServer}}   
  • In order to reveal your application, you need to edit the App Announcement:

      & Lt; Apps Android: Icon = "@ Drawable / Icon" Android: label = "@ string / app_name" Android: name = "com.yourpackage.YourApplication" & gt;  

If you frequently use Smartphone server objects, you can also use the Java 1.5 stable import feature:

  import static .yourpackage.YourApplication.smartFoxServer; Public activity myActivity {Public Zero Any method () {// Thanks for the steady imports, smartFoxServer getActivity can be accessed directly without the phone (). SmartFoxServer.doSomething (); }}  

I have written it here so that some things may be wrong ... I will see it in more details when children give me more than 2 minutes free time ;-)


Comments