I am implementing a real time taxi-hailing application with socket-io and in the implementation of driver application, we are dealing with socket problem. Even if the user closes the device screen, i just want to keep socket connection alive. Is there any specific permission for android?
Is there any suggestions about keeping the socket alive?
Sharing my permission lists.
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="ACCESS_WIFI_STATE"/>
<uses-permission android:name="ACCESS_NETWORK_STATE"/>
<uses-permission android:name="READ_PHONE_STATE"/>
<uses-permission android:name="SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
And my socket connection and socket handling codes are below. For connection:
public Observable<SocketConnection.State> connect(String customerId) {
if (mUserId == null || mUserType == null) {
return null;
}
if (mAddress == null) {
mAddress = Constants.DEFAULT_SOCKET_ADDRESS;
}
mNamespace = Constants.DEFAULT_SOCKET_NAMESPACE;
if (customerId != null) {
mNamespace = Constants.CUSTOMER_SOCKET_NAMESPACE;
}
if (socket != null) {
socket.disconnect();
removeSocketListeners();
}
try {
socket = IO.socket(mAddress + mNamespace, getOptions(customerId));
setSocketListeners();
startInternalPong();
} catch (URISyntaxException e) {
e.printStackTrace();
}
socket.connect();
return mConnectionObservable;
}
For disconnect:
public Observable<SocketConnection.State> disconnect() {
if (socket != null) {
removeSocketListeners();
socket.disconnect();
}
return mConnectionObservable;
}
Connection Observer:
private Observable<SocketConnection.State> mConnectionObservable = Observable.create(new ObservableOnSubscribe<SocketConnection.State>() {
@Override
public void subscribe(final ObservableEmitter<SocketConnection.State> observableEmitter) throws Exception {
mConnectionObservableEmitter = observableEmitter;
observableEmitter.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
mConnectionObservableEmitter = null;
}
});
}
});
You should take a look at Android Bacgrkound Services.
Also refer to these answers: