Java 无法连接到服务

Java 无法连接到服务,java,android,Java,Android,我正在尝试创建一个在我的应用程序中处理网络的服务。我按照和中的所有步骤进行了操作,但该活动似乎与服务没有联系 以下是SocketService,它具有使用套接字连接到我的服务器的TcpClient对象: public class SocketService extends Service{ TcpClient tcpClient = new TcpClient(); private final IBinder mBinder = new LocalBinder();

我正在尝试创建一个在我的应用程序中处理网络的服务。我按照和中的所有步骤进行了操作,但该活动似乎与服务没有联系

以下是SocketService,它具有使用套接字连接到我的服务器的TcpClient对象:

public class SocketService extends Service{

    TcpClient tcpClient = new TcpClient();

    private final IBinder mBinder = new LocalBinder();

    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        SocketService getService() {
            // Return this instance of LocalService so clients can call public methods
            return SocketService.this;
        }
    }

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

    /*
    * Client methods
    */

    public void connect(MyCallback callback, String ip, int port){
        tcpClient.connect(callback, ip, port);
    }

    public String disconnect(){
        return tcpClient.disconnect();
    }

    public String send(String data){
        return tcpClient.send(data);
    }

    public String recv(){
        return tcpClient.recv();
    }
}
以下是我的主要活动:

public class MainActivity extends AppCompatActivity {

    SocketService socketService;
    boolean bound = false;

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

    @Override
    protected void onStart(){
        super.onStart();

        Intent serviceIntent = new Intent(MainActivity.this, SocketService.class);

        if(bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)){
            Toast.makeText(getApplicationContext(), "bound", Toast.LENGTH_LONG);
        }

        if(bound) {
            socketService.connect(this, ip, port);
        } else {
            Toast.makeText(getApplicationContext(), "not bond", Toast.LENGTH_LONG).show();
        }
    }

    /*
    * Service callback
    */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                                   IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            Toast.makeText(getApplicationContext(), "ServiceConnection", Toast.LENGTH_LONG);
            socketService = ((SocketService.LocalBinder) service).getService();
            bound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            socketService = null;
            bound = false;
        }
    };
}
以及AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.drrbarrera.home">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".SocketService"
            android:enabled="true"
            android:exported="true">
        </service>
    </application>

</manifest>

正如我之前所说,主要活动似乎没有联系。“socketService”(在MainActivity中)保持为null,“bound”保持为false,就像没有执行“McConnection”一样。你知道什么地方出了问题吗? 感谢您的帮助。
提前谢谢

调用
bindService()
将返回一个布尔结果,告诉您绑定是否成功(实际上,它意味着正在进行)。操作是异步的,即使在同一个进程中也是如此(这就是您的
LocalBinder
的功能)

换句话说,在调用
ServiceConnection.onServiceConnected()
之前,绑定是不完整的。一旦该回调被点击,并且您获得了服务绑定器,那么您就可以调用支持服务

以下是一些帮助您解决问题的其他注意事项:

  • 由于
    服务
    活动
    在同一进程中运行,因此调用是直接的,不使用绑定线程。这将对您的
    活动产生影响
  • 阻止调用不应该是主(UI)线程上的主调用。这意味着如果您的
    活动
    代码要调用
    socketService.connect()
    ,则需要从后台线程执行。否则,您将得到一个异常,因为Android现在阻止主线程上的网络I/O。其他类型的阻塞操作可能会导致ANR,这将导致应用程序崩溃
  • 如果您的网络I/O用于REST或其他与HTTP相关的流量,请考虑使用改装或截击,因为它们具有高性能、可扩展性,并且可以为您处理与网络和HTTP相关的繁重任务

但启动mConnection时是否调用了ServiceConnection.OnServiceConnection()?否,绑定是一个异步操作
onServiceConnected()
将在任意绑定线程中回调,即使对于本地绑定也是如此。