使用完记得调用{@link #unregister()}
+ *需添加权限 {@code
需添加权限 {@code
需添加权限 {@code
如果{@code minDistance}为0,则通过{@code minTime}来定时更新;
+ *{@code minDistance}不为0,则以{@code minDistance}为准;
+ *两者都为0,则随时刷新。
+ * + * @param minTime 位置信息更新周期(单位:毫秒) + * @param minDistance 位置变化最小距离:当位置距离变化超过此值时,将更新位置信息(单位:米) + * @param listener 位置刷新的回调接口 + * @return {@code true}: 初始化成功Retry policy that retries a set number of times with increasing sleep time between retries
+// */ +//public class ExponentialBackOffRetry implements RetryPolicy { +// +// private static final int MAX_RETRIES_LIMIT = 29; +// private static final int DEFAULT_MAX_SLEEP_MS = Integer.MAX_VALUE; +// +// private final Random random = new Random(); +// private final long baseSleepTimeMs; +// private final int maxRetries; +// private final int maxSleepMs; +// +// public ExponentialBackOffRetry(int baseSleepTimeMs, int maxRetries) { +// this(baseSleepTimeMs, maxRetries, DEFAULT_MAX_SLEEP_MS); +// } +// +// public ExponentialBackOffRetry(int baseSleepTimeMs, int maxRetries, int maxSleepMs) { +// this.maxRetries = maxRetries; +// this.baseSleepTimeMs = baseSleepTimeMs; +// this.maxSleepMs = maxSleepMs; +// } +// +// @Override +// public boolean allowRetry(int retryCount) { +// if (retryCount < maxRetries) { +// return true; +// } +// return false; +// } +// +// @Override +// public long getSleepTimeMs(int retryCount) { +// if (retryCount < 0) { +// throw new IllegalArgumentException("retries count must greater than 0."); +// } +// if (retryCount > MAX_RETRIES_LIMIT) { +//// LogUtil.e(String.format("maxRetries too large (%d). Pinning to %d", maxRetries, MAX_RETRIES_LIMIT)); +// retryCount = MAX_RETRIES_LIMIT; +// } +// long sleepMs = baseSleepTimeMs * Math.max(1, random.nextInt(1 << retryCount)); +// if (sleepMs > maxSleepMs) { +//// LogUtil.e(String.format("Sleep extension too large (%d). Pinning to %d", sleepMs, maxSleepMs)); +// sleepMs = maxSleepMs; +// } +// return sleepMs; +// } +//} diff --git a/app/src/main/java/com/xinyingpower/microphoto/request/INettyMessageListener.java b/app/src/main/java/com/xinyingpower/microphoto/request/INettyMessageListener.java new file mode 100644 index 00000000..1fb65f2f --- /dev/null +++ b/app/src/main/java/com/xinyingpower/microphoto/request/INettyMessageListener.java @@ -0,0 +1,9 @@ +//package com.xinyingpower.microphoto.request; +// +//public interface INettyMessageListener { +// void onReceive(String message); +// +// void onConnectSuccess(); +// +// void onError(); +//} \ No newline at end of file diff --git a/app/src/main/java/com/xinyingpower/microphoto/request/NettyChatClient.java b/app/src/main/java/com/xinyingpower/microphoto/request/NettyChatClient.java new file mode 100644 index 00000000..3c67a632 --- /dev/null +++ b/app/src/main/java/com/xinyingpower/microphoto/request/NettyChatClient.java @@ -0,0 +1,116 @@ +//package com.xinyingpower.microphoto.request; +// +//import java.nio.charset.StandardCharsets; +//import java.util.concurrent.TimeUnit; +// +//import io.netty.bootstrap.Bootstrap; +//import io.netty.buffer.ByteBuf; +//import io.netty.buffer.Unpooled; +//import io.netty.channel.Channel; +//import io.netty.channel.ChannelFuture; +//import io.netty.channel.ChannelFutureListener; +//import io.netty.channel.ChannelInitializer; +//import io.netty.channel.ChannelOption; +//import io.netty.channel.ChannelPipeline; +//import io.netty.channel.EventLoopGroup; +//import io.netty.channel.nio.NioEventLoopGroup; +//import io.netty.channel.socket.SocketChannel; +//import io.netty.channel.socket.nio.NioSocketChannel; +//import io.netty.handler.codec.bytes.ByteArrayDecoder; +//import io.netty.handler.codec.bytes.ByteArrayEncoder; +//import io.netty.handler.codec.string.StringDecoder; +//import io.netty.handler.codec.string.StringEncoder; +//import io.netty.handler.timeout.IdleStateHandler; +// +//public class NettyChatClient { +// private static NettyChatClient mNettyChatClient; +// private static String mServerIp; +// private static int mPort; +// private Channel mChannel; +// /** +// * 重连策略 +// */ +// private RetryPolicy mRetryPolicy; +// private Bootstrap mBootstrap; +// +// public void init(INettyMessageListener messageListener) { +// mRetryPolicy = new ExponentialBackOffRetry(3000, Integer.MAX_VALUE, 3 * 1000); +// EventLoopGroup group = new NioEventLoopGroup();//初始化线程组 +// // bootstrap 可重用, 只需在TcpClient实例化的时候初始化即可. +// mBootstrap = new Bootstrap(); +// mBootstrap.group(group) +// .option(ChannelOption.TCP_NODELAY, true) +// .channel(NioSocketChannel.class) +// .handler(new ClientHandlersInitializer()); +// +// } +// +// public static NettyChatClient newInstance(String serverIp, int port) { +// if (mNettyChatClient == null) { +// mNettyChatClient = new NettyChatClient(); +// mServerIp = serverIp; +// mPort = port; +// } +// return mNettyChatClient; +// } +// +// public static NettyChatClient getInstance() { +// return mNettyChatClient; +// } +// +// /** +// * 向远程TCP服务器请求连接 +// */ +// public void connect() { +// synchronized (mBootstrap) { +// ChannelFuture future = mBootstrap.connect(mServerIp, mPort); +// future.addListener(getConnectionListener()); +// this.mChannel = future.channel(); +// } +// } +// +// private ChannelFutureListener getConnectionListener() { +// return future -> { +// if (!future.isSuccess()) { +// future.channel().pipeline().fireChannelInactive(); +// } +// }; +// } +// +// private class ClientHandlersInitializer extends ChannelInitializer