Reputation: 305
I have a bounded service which performs TCP Socket connection and sends some data repeatedly. After the device goes into sleep mode thread stops and so the data send. What am I doing wrong?
As far as I know, service bounded to the Main Activity has to be START_STICKY
but that doesn't fix my problem.
MainActivity class where the service is bounded and the connect button click starts socket sending thread in the 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";
}
}
The CTIController Service which controls the Socket connection:
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;
}
}
And my manifest file:
<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>
Upvotes: 0
Views: 60
Reputation: 5865
Do change in onStartCommand
of your service.
return START_REDELIVER_INTENT;
instead of return START_STICKY;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}
}
Upvotes: 1