[英]Not able to connect with a Service
我正在嘗試創建一個處理我的應用程序中的網絡的服務。 我按照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”一樣。 有什么問題的想法嗎? 任何幫助表示贊賞。 提前致謝!
對bindService()
的調用返回一個布爾結果,該結果告訴您綁定是否成功(實際上,它表示進行中 )。 該操作是異步的,即使在同一進程內(這也就是LocalBinder
所在的LocalBinder
)。
換句話說,只有在調用ServiceConnection.onServiceConnected()
之前,綁定才能完成。 一旦該回調被擊中,您便獲得了服務綁定器,然后就可以調用后備服務。
其他一些注意事項可以幫助您:
Service
與“ Activity
在同一進程中運行,因此調用是直接的,而不使用綁定線程。 這將影響您的Activity
代碼。 Activity
代碼要調用socketService.connect()
,則它將需要從后台線程執行此操作。 否則,您將得到一個例外,因為Android現在會阻止主線程上的網絡I / O。 其他類型的阻止操作可能會導致ANR,這將導致您的應用崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.