AppContext.java
5.68 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
package com.shunzhi.parent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.SDKOptions;
import com.netease.nimlib.sdk.StatusBarNotificationConfig;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.share.mvpsdk.global.GlobalApplication;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
/**
* Created by Administrator on 2018/3/7 0007.
*/
public class AppContext extends GlobalApplication {
private static AppContext appContext;
public final static int LOCATION_CITYNAME = 0x00;
public String cityName = "", district = "";
public static AppContext getInstance() {
return appContext;
}
//声明AMapLocationClient类对象
public AMapLocationClient mLocationClient = null;
//声明AMapLocationClientOption对象
public AMapLocationClientOption mLocationOption = null;
@Override
public void onCreate() {
appContext = this;
super.onCreate();
//开启地图地位
initMapLocal();
NIMClient.init(this, loginInfo(), options());
}
private SDKOptions options() {
SDKOptions options = new SDKOptions();
// 如果将新消息通知提醒托管给 SDK 完成,需要添加以下配置。否则无需设置。
StatusBarNotificationConfig config = new StatusBarNotificationConfig();
//config.notificationEntrance = MainActivity.class; // 点击通知栏跳转到该Activity
config.notificationSmallIconId = R.mipmap.ic_launcher;
options.sdkStorageRootPath = getExternalFilesDir(null).getPath() + "/nim";
options.statusBarNotificationConfig = config;
//options.preloadAttach=false;
return options;
}
private LoginInfo loginInfo() {
SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
String account = sp.getString("account", "");
String token = sp.getString("token", "");
if (account.length() > 0 && token.length() > 0) {
return new LoginInfo(account, token);
}
return null;
}
private void initMapLocal() {
//初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
//设置定位回调监听
mLocationClient.setLocationListener(mLocationListener);
//初始化AMapLocationClientOption对象
mLocationOption = new AMapLocationClientOption();
/**
* 设置定位场景,目前支持三种场景(签到、出行、运动,默认无场景)
*/
mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
//设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
//设置定位模式为AMapLocationMode.Battery_Saving,低功耗模式。
// mLocationOption.setLocationMode(AMapLocationMode.Battery_Saving);
//设置定位模式为AMapLocationMode.Device_Sensors,仅设备模式。
// mLocationOption.setLocationMode(AMapLocationMode.Device_Sensors);
//设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
mLocationOption.setInterval(1000);
//设置是否允许模拟位置,默认为true,允许模拟位置
mLocationOption.setMockEnable(true);
//单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
mLocationOption.setHttpTimeOut(20000);
//关闭缓存机制
mLocationOption.setLocationCacheEnable(false);
if (null != mLocationClient)
mLocationClient.setLocationOption(mLocationOption);
//启动定位
startLocation();
}
public void startLocation() {
//启动定位
if (null != mLocationClient) {
// mLocationClient.setLocationOption(mLocationOption);
//设置场景模式后最好调用一次stop,再调用start以保证场景模式生效
stopLocation();
mLocationClient.startLocation();
} else initMapLocal();
}
public void stopLocation() {
if (null != mLocationClient) mLocationClient.stopLocation();
}
public void destoryLocation() {
if (null != mLocationClient) mLocationClient.onDestroy();
}
//声明定位回调监听器
public AMapLocationListener mLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (null != aMapLocation) {
if (aMapLocation.getErrorCode() == 0) {
cityName = aMapLocation.getCity();
district = aMapLocation.getDistrict();
aMapLocation.getCityCode();
Intent intent = new Intent();
intent.setAction(LOCATION_CITYNAME + "");
sendBroadcast(intent);
stopLocation();
} else {
cityName = "定位失败";
// Log.d("mlocation:","errorCode="+aMapLocation.getErrorCode()+"errorInfo="+aMapLocation.getErrorInfo());
}
}
}
};
@Override
public void onLowMemory() {
super.onLowMemory();
//设置定位模式为AMapLocationMode.Battery_Saving,低功耗模式。
mLocationOption.setLocationMode(AMapLocationMode.Battery_Saving);
}
}