MyApplication.java
12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package com.chudiangameplay.android;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.http.HttpResponseCache;
import android.support.multidex.MultiDex;
import com.bumptech.glide.Glide;
import com.chudiangameplay.android.constant.SPConstants;
import com.chudiangameplay.android.manager.Config;
import com.chudiangameplay.android.manager.HMSPushHelper;
import com.chudiangameplay.android.service.InitializeService;
import com.chudiangameplay.android.tool.Log;
import com.chudiangameplay.android.tool.SPUtil;
import com.chudiangameplay.android.util.Util;
import com.fm.openinstall.OpenInstall;
import com.fm.openinstall.listener.AppInstallAdapter;
import com.fm.openinstall.model.AppData;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMOptions;
import com.hyphenate.push.EMPushConfig;
import com.hyphenate.push.EMPushHelper;
import com.hyphenate.push.EMPushType;
import com.hyphenate.push.PushListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.DBCookieStore;
import com.lzy.okgo.https.HttpsUtils;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.crashreport.CrashReport;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import okhttp3.OkHttpClient;
/**
* Created by Administrator on 2018/8/13.
*/
public class MyApplication extends Application {
public static Context context;
@Override
public void onCreate() {
super.onCreate();
Log.i("MyApplication.onCreate");
context = this;
int pid = android.os.Process.myPid();
String processAppName = getAppName(pid);
// 如果APP启用了远程的service,此application:onCreate会被调用2次
// 为了防止环信SDK被初始化2次,加此判断会保证SDK被初始化1次
// 默认的APP会在以包名为默认的process name下运行,如果查到的process name不是APP的process name就立即返回
if (processAppName == null || !processAppName.equalsIgnoreCase(context.getPackageName())) {
// 则此application::onCreate 是被service 调用的,直接返回
return;
}
initIM();
//初始化OkHttp
initOkgo();
InitializeService.start(this);
OpenInstall.init(this); //OpenInstall
//获取OpenInstall安装数据
OpenInstall.getInstall(new AppInstallAdapter() {
@Override
public void onInstall(AppData appData) {
//获取渠道数据
String channelCode = appData.getChannel();
//获取自定义数据
String bindData = appData.getData();
SPUtil.putValue(getBaseContext(), SPConstants.H5_ChannelCode, channelCode);
}
});
}
@Override
public void onTerminate() {
super.onTerminate();
// 程序终止的时候执行
Log.i("MyApplication.onTerminate");
}
@Override
public void attachBaseContext(Context base) {
MultiDex.install(base);
super.attachBaseContext(base);
}
@Override
public void onLowMemory() {
super.onLowMemory();
Glide.get(this).clearMemory(); //内存低的时候 清理Glide图片内存占用
}
private void initOkgo() {
//---------这里给出的是示例代码,告诉你可以这么传,实际使用的时候,根据需要传,不需要就不传-------------//
HttpHeaders headers = new HttpHeaders();
// headers.put("commonHeaderKey1", "commonHeaderValue1"); //header不支持中文,不允许有特殊字符
// headers.put("commonHeaderKey2", "commonHeaderValue2");
HttpParams params = new HttpParams();
// params.put("commonParamsKey1", "commonParamsValue1"); //param支持中文,直接传,不要自己编码
// params.put("commonParamsKey2", "这里支持中文参数");
//----------------------------------------------------------------------------------------//
OkHttpClient.Builder builder = new OkHttpClient.Builder();
//log相关
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
// loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.NONE); //log打印级别,决定了log显示的详细程度
if (Config.curVersion == Config.RELEASE) {
loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY); //log打印级别,决定了log显示的详细程度
} else {
loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
}
loggingInterceptor.setColorLevel(Level.WARNING); //log颜色级别,决定了log在控制台显示的颜色
builder.addInterceptor(loggingInterceptor); //添加OkGo默认debug日志
//第三方的开源库,使用通知显示当前请求的log,不过在做文件下载的时候,这个库好像有问题,对文件判断不准确
//builder.addInterceptor(new ChuckInterceptor(this));
//超时时间设置,默认60秒,当前设置为20秒
builder.readTimeout(10 * 1000, TimeUnit.MILLISECONDS); //全局的读取超时时间
builder.writeTimeout(10 * 1000, TimeUnit.MILLISECONDS); //全局的写入超时时间
builder.connectTimeout(10 * 1000, TimeUnit.MILLISECONDS); //全局的连接超时时间
//自动管理cookie(或者叫session的保持),以下几种任选其一就行
//builder.cookieJar(new CookieJarImpl(new SPCookieStore(this))); //使用sp保持cookie,如果cookie不过期,则一直有效
builder.cookieJar(new CookieJarImpl(new DBCookieStore(this))); //使用数据库保持cookie,如果cookie不过期,则一直有效
//builder.cookieJar(new CookieJarImpl(new MemoryCookieStore())); //使用内存保持cookie,app退出后,cookie消失
//https相关设置,以下几种方案根据需要自己设置
//方法一:信任所有证书,不安全有风险
HttpsUtils.SSLParams sslParams1 = HttpsUtils.getSslSocketFactory();
//方法二:自定义信任规则,校验服务端证书
// HttpsUtils.SSLParams sslParams2 = HttpsUtils.getSslSocketFactory(new SafeTrustManager());
//方法三:使用预埋证书,校验服务端证书(自签名证书)
//HttpsUtils.SSLParams sslParams3 = HttpsUtils.getSslSocketFactory(getAssets().open("srca.cer"));
//方法四:使用bks证书和密码管理客户端证书(双向认证),使用预埋证书,校验服务端证书(自签名证书)
//HttpsUtils.SSLParams sslParams4 = HttpsUtils.getSslSocketFactory(getAssets().open("xxx.bks"), "123456", getAssets().open("yyy.cer"));
builder.sslSocketFactory(sslParams1.sSLSocketFactory, sslParams1.trustManager);
//配置https的域名匹配规则,详细看demo的初始化介绍,不需要就不要加入,使用不当会导致https握手失败
// builder.hostnameVerifier(new SafeHostnameVerifier());
// 其他统一的配置
// 详细说明看GitHub文档:https://github.com/jeasonlzy/
OkGo.getInstance().init(this) //必须调用初始化
.setOkHttpClient(builder.build()) //建议设置OkHttpClient,不设置会使用默认的
.setCacheMode(CacheMode.NO_CACHE) //全局统一缓存模式,默认不使用缓存,可以不传
.setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE) //全局统一缓存时间,默认永不过期,可以不传
.setRetryCount(2); //全局统一超时重连次数,默认为三次,那么最差的情况会请求4次(一次原始请求,三次重连请求),不需要可以设置为0
// .addCommonHeaders(headers) //全局公共头
// .addCommonParams(params); //全局公共参数
File cacheDir = new File(Util.getDiskCacheRootDir(context), "http");
try {
HttpResponseCache.install(cacheDir, 1024 * 1024 * 128);
} catch (IOException e) {
e.printStackTrace();
}
}
private void initIM() {
Log.i("初始化IM");
try {
EMPushConfig.Builder builder = new EMPushConfig.Builder(context);
builder
// .enableVivoPush() // 推送证书相关信息配置在AndroidManifest.xml中,vivo申请不到证书
.enableMeiZuPush("121430", "cfb42b6fcab747a88f98694876314b9c") //魅族
.enableMiPush("2882303761517910570", "5161791085570") //小米(AppID,AppKey)
.enableOppoPush("a41a88004cd8455bb5c728901b94d493", "364000e1b71f431ca6237a5facd65ed4") //oppo (AppKey,AppSecret)
.enableHWPush() //开发者需要调用该方法来开启华为推送
// .enableFCM(String senderId) //开发者需要调用该方法来开启谷歌FCM推送
;
EMOptions options = new EMOptions();
options.setPushConfig(builder.build());
options.setAutoLogin(true);
// 默认添加好友时,是不需要验证的,改成需要验证
options.setAcceptInvitationAlways(false);
// 是否自动将消息附件上传到环信服务器,默认为True是使用环信服务器上传下载,如果设为 false,需要开发者自己处理附件消息的上传和下载
options.setAutoTransferMessageAttachments(true);
// 是否自动下载附件类消息的缩略图等,默认为 true 这里和上边这个参数相关联
options.setAutoDownloadThumbnail(true);
//初始化
EMClient.getInstance().init(getApplicationContext(), options);
//在做打包混淆时,关闭debug模式,避免消耗不必要的资源
EMClient.getInstance().setDebugMode(false);
// 初始化华为 HMS 推送服务, 需要在SDK初始化后执行
HMSPushHelper.getInstance().initHMSAgent(this);
EMPushHelper.getInstance().setPushListener(new PushListener() {
@Override
public void onError(EMPushType pushType, long errorCode) {
Log.i("推送设置回调 onError:pushType:"+ pushType+ " errorCode:"+ errorCode);
// TODO: 开发者会在这个回调中收到使用推送的相关错误信息,各推送类型的error code开发者可以自己去各推送平台官网查询错误原因。
}
@Override
public boolean isSupportPush(EMPushType pushType, EMPushConfig pushConfig) {
Log.i("推送设置回调 pushType:"+ pushType+ " pushConfig:"+ pushConfig.toString());
return super.isSupportPush(pushType, pushConfig);
// TODO:开发者可以复写该方法控制设备是否支持某推送的判断。
}
});
} catch (Exception e) {
Log.i("推送设置回调 exception:"+ e.toString());
e.printStackTrace();
}
}
private String getAppName(int pID) {
String processName = null;
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
try {
if (info.pid == pID) {
processName = info.processName;
return processName;
}
} catch (Exception e) {
// Log.d("Process", "Error>> :"+ e.toString());
}
}
return processName;
}
}