我有一个有界服务,它执行 TCP 套接字连接并重复发送一些数据。设备进入休眠模式后,线程停止,因此数据发送。我做错了什么?
据我所知,绑定(bind)到主 Activity 的服务必须是START_STICKY
,但这并不能解决我的问题。
MainActivity 类,其中服务受到限制,单击连接按钮会在 AsyncTask
中启动套接字发送线程:
public class MainActivity extends AppCompatActivity {
public MainActivity() {
super();
}
private CTIController CTIControl;
protected boolean mBound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, CTIController.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if (mBound) {
//disconnect socket and stop sending thread
CTIControl.GetControllersFactory().GetCTI2Controller().OnDisconnect();
unbindService(mConnection);
mBound = false;
}
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
LocalBinder binder = (LocalBinder) service;
CTIControl = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
};
/** Callled when the user clicks connect button */
public void ConnectClick(View view) {
new ConnectOperation().execute("");
}
private class ConnectOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
EditText ip = (EditText)findViewById(R.id.ip);
EditText port = (EditText)findViewById(R.id.port);
//start socket connection and run sending thread
CTIControl.TryConnect(ip.getText().toString(), port);
return "Executed";
}
}
控制 Socket 连接的 CTIController 服务:
public class CTIController extends Service implements ICTIController {
private final IBinder mBinder = new LocalBinder();
public TryConnect(String ipAddress, int port) throws IOException{
this.ClientSocket = new Socket();
this.ClientSocket.connect(new InetSocketAddress(ipAddress, port), 10000);
//... start sending data repeatedly
}
public class LocalBinder extends Binder {
public CTIController getService() {
return CTIController.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
}
还有我的 list 文件:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.PERSISTENT_ACTIVITY" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<application
android:allowBackup="true"
android:persistent="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name=".controller.CTIController" android:exported="false"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最佳答案
请更改服务的 onStartCommand
。
返回 START_REDELIVER_INTENT;
而不是返回 START_STICKY;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}
}
关于java - TCPSocket 服务在设备 sleep 模式下停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39365285/