Java tutorial
/** * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.minglang.suiuu.application; import android.content.Context; import android.content.Intent; import android.os.StrictMode; import android.support.multidex.MultiDex; import android.support.v4.content.LocalBroadcastManager; import com.alibaba.sdk.android.oss.OSSService; import com.alibaba.sdk.android.oss.OSSServiceProvider; import com.alibaba.sdk.android.oss.model.AccessControlList; import com.alibaba.sdk.android.oss.model.ClientConfiguration; import com.alibaba.sdk.android.oss.model.TokenGenerator; import com.alibaba.sdk.android.oss.util.OSSToolKit; import com.facebook.drawee.backends.pipeline.Fresco; import com.koushikdutta.WebSocketClient; import com.minglang.suiuu.utils.L; import com.minglang.suiuu.utils.http.HttpNewServicePath; import com.tencent.bugly.crashreport.CrashReport; import org.apache.http.message.BasicNameValuePair; import org.lasque.tusdk.core.TuSdkApplication; import java.net.URI; import java.util.Collections; import java.util.List; public class SuiuuApplication extends TuSdkApplication { private static final String TAG = SuiuuApplication.class.getSimpleName(); public static final String CONNECT = "connect"; public static final String STRING_MESSAGE = "String_Message"; public static final String BYTE_MESSAGE = "Byte_Message"; public static final String DISCONNECT = "disconnect"; public static final String DISCONNECT_CODE = "disconnect_code"; public static final String ERROR = "error"; private static SuiuuApplication instance; /** * ?nickname,??userid */ public static OSSService ossService = OSSServiceProvider.getService(); static final String accessKey = "LaKLZHyL2Dmy8Qqq"; // ?AK/SK static final String screctKey = "c7xPteQRqjV8nNB8xGFIZoFijzjDLX"; private static WebSocketClient webSocketClient; private LocalBroadcastManager localBroadcastManager; public static SuiuuApplication getInstance() { if (instance == null) { instance = new SuiuuApplication(); } return instance; } @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this);//?Dex? } @SuppressWarnings({ "ResultOfMethodCallIgnored", "deprecation" }) @Override public void onCreate() { super.onCreate(); initApplication(); initAboutOSS(); CrashReport.initCrashReport(this, "900011358", false); //GlobalCrashHandler.getInstance().init(this); List<BasicNameValuePair> extraHeaders = Collections.singletonList(new BasicNameValuePair("", "")); try { webSocketClient = new WebSocketClient(URI.create(HttpNewServicePath.SocketPath), new InitListener(), extraHeaders); } catch (Exception e) { L.e(TAG, "WebSocketClient?:" + e.getMessage()); } try { webSocketClient.connect(); } catch (Exception e) { L.e(TAG, "WebSocketClient:" + e.getMessage()); } } private void initApplication() { instance = this; localBroadcastManager = LocalBroadcastManager.getInstance(this); //fresco? Fresco.initialize(this); //?TuSDK this.initPreLoader(this, "745f61271fd7f7f7-00-04gxn1"); // ? this.setEnableLog(false); } /** * ?OSS */ public void initAboutOSS() { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); // ? ossService.setApplicationContext(this); ossService.setGlobalDefaultTokenGenerator(new TokenGenerator() { // @Override public String generateToken(String httpMethod, String md5, String type, String date, String ossHeaders, String resource) { String content = httpMethod + "\n" + md5 + "\n" + type + "\n" + date + "\n" + ossHeaders + resource; return OSSToolKit.generateToken(accessKey, screctKey, content); } }); ossService.setGlobalDefaultHostId("oss-cn-hongkong.aliyuncs.com"); ossService.setCustomStandardTimeWithEpochSec(System.currentTimeMillis() / 1000); ossService.setGlobalDefaultACL(AccessControlList.PRIVATE); // private ClientConfiguration conf = new ClientConfiguration(); conf.setConnectTimeout(15 * 1000); // 30s conf.setSocketTimeout(15 * 1000); // socket30s conf.setMaxConnections(50); // ?, 50 ossService.setClientConfiguration(conf); } public static WebSocketClient getWebSocketClient() { return webSocketClient; } private class InitListener implements WebSocketClient.Listener { @Override public void onConnect() { L.i(TAG, "onConnect()"); Intent intent = new Intent(CONNECT); intent.putExtra(CONNECT, "Socket"); localBroadcastManager.sendBroadcast(intent); } @Override public void onMessage(String message) { L.i(TAG, "String Message:" + message); Intent intent = new Intent(STRING_MESSAGE); intent.putExtra(STRING_MESSAGE, message); localBroadcastManager.sendBroadcast(intent); } @Override public void onMessage(byte[] data) { String str = new String(data); L.i(TAG, "byte[] Message:" + str); Intent intent = new Intent(BYTE_MESSAGE); intent.putExtra(BYTE_MESSAGE, str); localBroadcastManager.sendBroadcast(intent); } @Override public void onDisconnect(int code, String reason) { L.i(TAG, "onDisconnect(),code:" + code + ",reason:" + reason); Intent intent = new Intent(DISCONNECT); intent.putExtra(DISCONNECT_CODE, code); intent.putExtra(DISCONNECT, reason); localBroadcastManager.sendBroadcast(intent); } @Override public void onError(Exception error) { L.e(TAG, "onError(),error:" + error.getMessage()); Intent intent = new Intent(ERROR); intent.putExtra(ERROR, error); localBroadcastManager.sendBroadcast(intent); } } }