我正在使用 socket-io 实现一个实时打车应用程序,在驱动程序应用程序的实现中,我们正在处理套接字问题。即使用户关闭设备屏幕,我也只想保持套接字连接处于活动状态。android有什么特定的权限吗?
有没有关于保持套接字活动的建议?
分享我的权限列表。
<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"/>
我的套接字连接和套接字处理代码如下。对于连接:
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;
}
对于断开连接:
public Observable<SocketConnection.State> disconnect() {
if (socket != null) {
removeSocketListeners();
socket.disconnect();
}
return mConnectionObservable;
}
连接观察者:
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;
}
});
}
});