0

how can i keep the socket active on the service, when i close the app it disconnected, from node, i call it from the main activity, with startService(new Intent(this, sys_service.class)); the socket its fine while the app its active

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URISyntaxException;

public class sys_service extends Service {
     Socket mSocket;
    TelephonyManager manager;
    JSONObject chat_response;
    private Handler mHandler = new Handler();
    String msg_over;
    {
        try {
            mSocket = IO.socket("http://xxxx:8001");

        } catch (URISyntaxException e) {
            Toast.makeText(this, "refused", Toast.LENGTH_LONG).show();

        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {
        Toast.makeText(this,"Service Active :)", Toast.LENGTH_LONG ).show();

        run_app();
        return START_STICKY;
    }


    @Override
    public void onDestroy() {
        Toast.makeText(this,"Service stop :)", Toast.LENGTH_LONG ).show();

        super.onDestroy();
    }


    @Override
    public IBinder onBind(Intent intent){
        run_app();
        return null;
    }


    public Socket getSocket() {
        return mSocket;
    }

    public void run_app() {
        mSocket.connect();
        manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        final JSONObject uidJson = new JSONObject();
        String real = manager.getDeviceId();

        try {
            uidJson.put("uid", real);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        mSocket.on("connect", new Emitter.Listener() {

            public void call(Object... args) {
                mSocket.emit("uid", uidJson);

            }


        });


        mSocket.on("chat", new Emitter.Listener() {
            public void call(Object... args) {
                String chat_msg = args[0].toString();
                try {
                    chat_response = new JSONObject(chat_msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                msg_over = chat_response.optString("msg");


                final JSONObject chat = new JSONObject();


                try {
                    chat.put("msg", "all fine");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                mSocket.emit("test", msg_over);
                //Toast.makeText(StartActivity.this, msg, Toast.LENGTH_SHORT).show();  Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();


                Log.d("chat", msg_over);
            }


        });


    }




}
  • Have you obtained a Wake Lock? If you haven't Check [this](https://developer.android.com/training/scheduling/wakelock.html) and [this](http://stackoverflow.com/questions/2039555/how-to-get-an-android-wakelock-to-work). – Abbas Aug 11 '16 at 04:53
  • hi and thank you, i tried but no luck , i been reading and reading and cant find the problem. – Alexis Rodriguez Aug 11 '16 at 06:37
  • the service on app close still running, but socket doesn't. – Alexis Rodriguez Aug 11 '16 at 06:56
  • Why don't you post the code where you added a wake lock. Also check [this](http://android.stackexchange.com/questions/112503/download-stops-when-screen-off) – Abbas Aug 11 '16 at 06:59
  • I put the run_app(); before of super.onCreate(); and it works, and uncomment the wakelock – Alexis Rodriguez Aug 11 '16 at 16:15
  • i note that i have to uninstall the app before every run or debug, if i dont do that, the service die on background in AVD, i will make more test of this. Thank you. – Alexis Rodriguez Aug 11 '16 at 16:22

0 Answers0