我正在尝试创建一个处理我的应用程序中网络的服务。我遵循了https://developer.android.com/reference/android/app/Service和https://developer.android.com/guide/components/bound-services#Binding中的所有步骤,但是活动似乎与服务无关。
这是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>
正如我之前所说,MainActivity似乎没有连接。 (在MainActivity中)“ socketService”保持为空,而“ bound”保持为false,就像未执行“ mConnection”一样。有什么问题的想法吗? 任何帮助表示赞赏。 预先感谢!
答案 0 :(得分:1)
对bindService()
的调用返回一个布尔结果,告诉您绑定是否成功(实际上,这表示正在进行中)。该操作是异步的,即使在同一进程中(这就是您的LocalBinder
也是这样)。
换句话说,只有在您的ServiceConnection.onServiceConnected()
被回叫之前,绑定才能完成。一旦该回调被击中并且您获得了服务绑定程序,就可以调用后备服务。
其他一些注释可以帮助您:
Service
与Activity
的运行过程相同,因此调用是直接的,并且不使用绑定线程。这将影响您的Activity
代码。Activity
代码要调用socketService.connect()
,则需要从后台线程执行此操作。否则,您将得到一个例外,因为Android现在会阻止主线程上的网络I / O。其他类型的阻止操作可能会导致ANR,这将导致您的应用崩溃。