| | |
| | | package cn.sinata.rxnetty; |
| | | |
| | | import static cn.sinata.rxnetty.LogHelper.logRecord; |
| | | |
| | | import android.app.job.JobScheduler; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.os.Build; |
| | | import android.util.Log; |
| | | |
| | | import java.util.ArrayList; |
| | | |
| | |
| | | */ |
| | | |
| | | public class NettyClient { |
| | | |
| | | private String TAG = "NettyClient"; |
| | | public boolean isStop = false; |
| | | private Context mContext; |
| | | private ArrayList<OnMessageListener> listeners ; |
| | | private ArrayList<OnMessageListener> listeners; |
| | | private OnSendListener sendListener; |
| | | private OnConnectListener connectListener; |
| | | private OnCheckListener onCheckListener; |
| | | |
| | | public void init(Context context,String server,int port) { |
| | | init(context,server,port,false); |
| | | } |
| | | public void init(Context context, String server, int port) { |
| | | init(context, server, port, false); |
| | | } |
| | | |
| | | public void init(Context context,String server,int port,boolean isStartForeground) { |
| | | public void init(Context context, String server, int port, boolean isStartForeground) { |
| | | mContext = context.getApplicationContext(); |
| | | Config.SOCKET_SERVER = server; |
| | | Config.SOCKET_PORT = port; |
| | | Config.isStartForeground= isStartForeground; |
| | | Config.isStartForeground = isStartForeground; |
| | | } |
| | | |
| | | public OnConnectListener getConnectListener() { |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 添加消息监听 |
| | | * |
| | | * @param listener |
| | | */ |
| | | public void addOnMessageListener(OnMessageListener listener) { |
| | |
| | | |
| | | /** |
| | | * 连接完成监听 |
| | | * |
| | | * @param listener |
| | | */ |
| | | public void setOnConnectListener(OnConnectListener listener) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 移除消息监听 |
| | | * 移除消息监听 |
| | | * |
| | | * @param listener |
| | | */ |
| | | public void removeOnMessageListener(OnMessageListener listener) { |
| | |
| | | |
| | | /** |
| | | * 发送消息给服务器 |
| | | * |
| | | * @param msg |
| | | */ |
| | | public void sendMessage(String msg) { |
| | | Log.e("sendLocation: ","发送消息" +msg); |
| | | logRecord(TAG, "sendMessage sendListener is null " + (sendListener == null) + " msg = " + msg); |
| | | if (sendListener != null) { |
| | | sendListener.onSend(msg); |
| | | } |
| | |
| | | /** |
| | | * 检测netty连接状况 |
| | | */ |
| | | public void checkNettyState() { |
| | | public boolean checkNettyState() { |
| | | logRecord(TAG, "checkNettyState onCheckListener is null " + (onCheckListener == null)); |
| | | if (onCheckListener != null) { |
| | | onCheckListener.doCheck(); |
| | | return onCheckListener.doCheck(); |
| | | } else { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | |
| | | return; |
| | | } |
| | | if (!isStop) { |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| | | try { |
| | | Intent intent = new Intent(this.getContext(), NJobService.class); |
| | | this.mContext.stopService(intent); |
| | |
| | | } |
| | | } |
| | | isStop = false; |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| | | try { |
| | | Intent intent = new Intent(this.getContext(), NJobService.class); |
| | | this.mContext.startService(intent); |
| | |
| | | return; |
| | | } |
| | | isStop = true; |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| | | try { |
| | | JobScheduler jobScheduler = (JobScheduler) this.mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE); |
| | | if(jobScheduler!=null) |
| | | jobScheduler.cancel(11); |
| | | if (jobScheduler != null) jobScheduler.cancel(11); |
| | | this.mContext.stopService(new Intent(this.mContext, NJobService.class)); |
| | | this.mContext.stopService(new Intent(this.mContext, CoreService.class)); |
| | | } catch (Exception e) { |