Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 套接字对象在绑定到服务的活动中返回null_Android_Sockets_Android Activity_Android Service - Fatal编程技术网

Android 套接字对象在绑定到服务的活动中返回null

Android 套接字对象在绑定到服务的活动中返回null,android,sockets,android-activity,android-service,Android,Sockets,Android Activity,Android Service,我有一个关于通过服务实例进入Android活动的套接字对象的问题 套接字对象总是返回null,我不明白为什么。 我在其他活动中使用相同的方法,效果很好。有人能看一下吗 这是我的密码: **Service class code:** public class SocketService extends Service { // Create subclass of IBinder and bind it to local class instance private final IBin

我有一个关于通过服务实例进入Android活动的套接字对象的问题

套接字对象总是返回null,我不明白为什么。 我在其他活动中使用相同的方法,效果很好。有人能看一下吗

这是我的密码:

**Service class code:**

public class SocketService extends Service {
    // Create subclass of IBinder and bind it to local class instance
private final IBinder mBinder = new LocalBinder();      
public class LocalBinder extends Binder { 
    SocketService getService() { 
        return SocketService.this; 
        }
    }

@Override
public IBinder onBind(Intent arg0) {
    return mBinder;
}

@Override
public void onCreate() {
        try {
            socket = new SocketIO(URL);
            socket.connect(new IOCallback() {

                // Tell if connection was established
                @Override
                public void onConnect(){    
                }

                // Server Message
                @Override
                public void on(String event, final IOAcknowledge ack,
                        Object... arg) {
            }

                @Override
                public void onDisconnect() {
                    socket.reconnect();
                }

                @Override
                public void onError(SocketIOException arg0) {
                    Log.e("ERROR", "No Connection to host");
                    error = true;
            }    

                @Override
                public void onMessage(String arg0, IOAcknowledge arg1) {        
                }

                @Override
                public void onMessage(JSONObject arg0, IOAcknowledge arg1) {    
                }   
            });

        } catch (MalformedURLException e) {
            Log.e("error", "Wrong Server URL" + e); }
        } else {
            Log.e("ERROR", "No connection possible");
            }
}

@Override
public void onDestroy() {
    super.onDestroy();
    socket.disconnect();
    stopSelf();
}

public SocketIO getSocket(){
    return socket;
}
这里是带有空套接字对象的活动:

Activity code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

 socketConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            socketService = binder.getService();
            isBound = true;
        }

        public void onServiceDisconnected(ComponentName arg0) {
            socketService = null;
        }
       };

        // Bind to socket service
        Intent serviceIntent = new Intent(ContactMenuActivity.this, SocketService.class);
        bindService(serviceIntent, socketConnection, Context.BIND_AUTO_CREATE);


        socket = socketService.getSocket(); // This code always returns null 
but only for this activity, it works with other activities with same approach

好的,我找到了我问题的答案。显然,绑定到服务可能需要一点时间,在这两种情况下都比启动我的活动要长

解决方案是在intent连接到服务后立即检查IBinder对象的返回值:

Service Class

@Override
public IBinder onBind(Intent arg0) {
    Log.d("DEBUG", "Connected to service");
    return mBinder;
}

Activity Class

if (mBinder != null)
socket = socketService.getSocket();