/**
* 灰色保活手法创建的Service进程
*
* @author Clock
* @since 2016-04-12
*/
public class GrayService extends Service {
private final static String TAG = GrayService.class.getSimpleName();
private final static int GRAY_SERVICE_ID = -1001;
private OkHttpClient httpClient;
private WebSocket webSocket;
private final IBinder binder = new MyBinder();
@Override
public void onCreate() {
Log.i(TAG, "GrayService->onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "GrayService->onStartCommand");
// if (Build.VERSION.SDK_INT < 18) {
// startForeground(GRAY_SERVICE_ID, new Notification());//API < 18 ,此方法能有效隐藏Notification上的图标
// } else {
// Intent innerIntent = new Intent(this, GrayInnerService.class);
// startService(innerIntent);
// startForeground(GRAY_SERVICE_ID, new Notification());
// }
//发送唤醒广播来促使挂掉的UI进程重新启动起来
// AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Intent alarmIntent = new Intent();
// alarmIntent.setAction(WakeReceiver.GRAY_WAKE_ACTION);
// PendingIntent operation = PendingIntent.getBroadcast(this, WAKE_REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), ALARM_INTERVAL, operation);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "GrayService->onBind");
httpClient = HttpClient.getOkHttpClient();
Request request = new Request.Builder().url("ws://124.222.224.186:8800").build();
webSocket = httpClient.newWebSocket(request, new WebSocketListener() {
@Override
public void onClosed(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
super.onClosed(webSocket, code, reason);
}
@Override
public void onClosing(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
super.onClosing(webSocket, code, reason);
}
@Override
public void onFailure(@NonNull WebSocket webSocket, @NonNull Throwable t, @Nullable Response response) {
super.onFailure(webSocket, t, response);
Log.i(TAG, "onFailure Throwable = " + t.getMessage() + " response" + (response == null ? null : response.code()));
}
@Override
public void onMessage(@NonNull WebSocket webSocket, @NonNull String text) {
super.onMessage(webSocket, text);
Log.i(TAG, "onMessage text = " + text);
}
@Override
public void onMessage(@NonNull WebSocket webSocket, @NonNull ByteString bytes) {
super.onMessage(webSocket, bytes);
Log.i(TAG, "onMessage bytes = " + bytes);
}
@Override
public void onOpen(@NonNull WebSocket webSocket, @NonNull Response response) {
super.onOpen(webSocket, response);
Log.i(TAG, "onOpen" + response.code());
}
});
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
webSocket.send("testhyy");
}
};
timer.scheduleAtFixedRate(timerTask, 0, 1000);
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "GrayService->onUnbind");
stopSelf();
if (webSocket != null) {
webSocket.close(1000, "Service is being onUnbind");
}
if (httpClient != null) {
httpClient.dispatcher().executorService().shutdown();
}
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.i(TAG, "GrayService->onDestroy");
super.onDestroy();
if (webSocket != null) {
webSocket.close(1000, "Service is being destroyed");
}
if (httpClient != null) {
httpClient.dispatcher().executorService().shutdown();
}
}
/**
* 给 API >= 18 的平台上用的灰色保活手段
*/
public static class GrayInnerService extends Service {
@Override
public void onCreate() {
Log.i(TAG, "InnerService -> onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "InnerService -> onStartCommand");
startForeground(GRAY_SERVICE_ID, new Notification());
//stopForeground(true);
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "InnerService -> onStartCommand" + intent);
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
Log.i(TAG, "InnerService -> onDestroy");
super.onDestroy();
}
}
public class MyBinder extends Binder {
GrayService getService() {
// 返回当前 Service 实例,以便客户端调用公共方法
return GrayService.this;
}
}
}
Service websocket
最新推荐文章于 2024-09-06 09:02:52 发布