PageRenderTime 1801ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/dateLoveInternational/src/main/java/com/datelove/online/International/activity/SplashActivity.java

https://bitbucket.org/juzhiwiscom/datelove
Java | 764 lines | 593 code | 74 blank | 97 comment | 150 complexity | ee6e7e381118b7b78620c4ca45280d93 MD5 | raw file
  1. package com.datelove.online.International.activity;
  2. import android.Manifest;
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7. import android.location.Location;
  8. import android.location.LocationListener;
  9. import android.location.LocationManager;
  10. import android.net.ConnectivityManager;
  11. import android.net.NetworkInfo;
  12. import android.net.Uri;
  13. import android.os.Build;
  14. import android.os.Bundle;
  15. import android.os.Handler;
  16. import android.provider.Settings;
  17. import android.support.annotation.NonNull;
  18. import android.support.v4.app.ActivityCompat;
  19. import android.support.v4.content.ContextCompat;
  20. import android.text.TextUtils;
  21. import android.util.Log;
  22. import android.view.View;
  23. import com.alibaba.fastjson.JSON;
  24. import com.alibaba.fastjson.JSONArray;
  25. import com.alibaba.fastjson.JSONObject;
  26. import com.appsflyer.AppsFlyerLib;
  27. import com.baidu.android.pushservice.PushConstants;
  28. import com.baidu.android.pushservice.PushManager;
  29. import com.crashlytics.android.Crashlytics;
  30. import com.datelove.online.International.R;
  31. import com.datelove.online.International.base.BaseFragmentActivity;
  32. import com.datelove.online.International.bean.CheckRefresh;
  33. import com.datelove.online.International.bean.LocationInfo;
  34. import com.datelove.online.International.bean.LoginEntity;
  35. import com.datelove.online.International.bean.PlatformInfo;
  36. import com.datelove.online.International.constant.IConfigConstant;
  37. import com.datelove.online.International.constant.IUrlConstant;
  38. import com.datelove.online.International.utils.AppsflyerUtils;
  39. import com.datelove.online.International.utils.Cheeses;
  40. import com.datelove.online.International.utils.CommonRequestUtil;
  41. import com.datelove.online.International.utils.CountryFidUtils;
  42. import com.datelove.online.International.xml.PlatformInfoXml;
  43. import com.datelove.online.International.xml.UserInfoXml;
  44. import com.library.dialog.OnDoubleDialogClickListener;
  45. import com.library.dialog.OnSingleDialogClickListener;
  46. import com.library.utils.DensityUtil;
  47. import com.library.utils.DialogUtil;
  48. import com.library.utils.SharedPreferenceUtil;
  49. import com.library.utils.TimeUtil;
  50. import com.library.utils.ToastUtil;
  51. import com.library.utils.Util;
  52. import com.library.utils.VersionInfoUtil;
  53. import com.zhy.http.okhttp.OkHttpUtils;
  54. import com.zhy.http.okhttp.callback.Callback;
  55. import com.zhy.http.okhttp.callback.FileCallBack;
  56. import com.zhy.http.okhttp.callback.StringCallback;
  57. import java.io.File;
  58. import java.net.Inet4Address;
  59. import java.net.InetAddress;
  60. import java.net.NetworkInterface;
  61. import java.util.Enumeration;
  62. import java.util.HashMap;
  63. import java.util.Map;
  64. import io.fabric.sdk.android.Fabric;
  65. import okhttp3.Call;
  66. import okhttp3.Response;
  67. /**
  68. * Created by Administrator on 2016/10/18.
  69. */
  70. public class SplashActivity extends BaseFragmentActivity {
  71. private String UPDATE_SERVERAPK = "Flirt&Hookup";
  72. private LocationManager mLocationManager;
  73. private static final int PERMISSION_CODE_ACCESS_FINE_LOCATION = 0;
  74. // 埋点:获取用户对定位权限的态度:1定位成功,2定位失败,3关闭定位
  75. private static final String POSITION_SUCCESS = "1";
  76. private static final String POSITION_FAIL = "2";
  77. private static final String CLOSE_POSITION = "3";
  78. private String getCountry;
  79. private boolean isFinish=false;
  80. private boolean removeLocation=false;
  81. private boolean isfirstGetLoaction=true;
  82. @Override
  83. protected int getLayoutResId() {
  84. return R.layout.activity_splash;
  85. }
  86. @Override
  87. protected void initViewsAndVariables() {
  88. // bug统计工具
  89. Fabric.with(this, new Crashlytics());
  90. // 百度推送service初始化及绑定
  91. PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, IConfigConstant.BAIDU_PUSH_API_KEY);
  92. // AppsFlyer初始化
  93. AppsFlyerLib.getInstance().setAndroidIdData(getAndroidId());
  94. AppsFlyerLib.getInstance().startTracking(this.getApplication(), IConfigConstant.APPSFLYER_DEV_KEY);
  95. // 获得platform信息
  96. getPlatformInfo();
  97. // 获取位置信息并上传
  98. requestLocation();
  99. }
  100. @Override
  101. protected void addListeners() {
  102. }
  103. /**
  104. * 检查GPS定位开关
  105. *
  106. * @return
  107. */
  108. private boolean checkGPS() {
  109. if (mLocationManager != null) {
  110. return (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)||mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
  111. }
  112. return false;
  113. }
  114. public void checkRefresh() {
  115. OkHttpUtils.post()
  116. .url(IUrlConstant.URL_CHECK_UPDATE)
  117. .addHeader("token", PlatformInfoXml.getToken())
  118. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  119. .build()
  120. .execute(new RefreshCallBack());
  121. }
  122. private ProgressDialog pd;
  123. private class RefreshCallBack extends Callback<CheckRefresh> {
  124. @Override
  125. public CheckRefresh parseNetworkResponse(Response response, int id) throws Exception {
  126. String resultJson = response.body().string();
  127. if (!TextUtils.isEmpty(resultJson)) {
  128. return JSON.parseObject(resultJson, CheckRefresh.class);
  129. }
  130. return null;
  131. }
  132. @Override
  133. public void onError(Call call, Exception e, int id) {
  134. login();
  135. }
  136. @Override
  137. public void onResponse(CheckRefresh response, int id) {
  138. if (response != null) {
  139. if (response.isUpdate == null || response.isUpdate.equals("0")) {
  140. login();
  141. } else {
  142. DialogUtil.showSingleBtnDialog(getSupportFragmentManager(), "Update", response.updateInfo,
  143. getString(R.string.positive), false, new OnSingleDialogClickListener() {
  144. @Override
  145. public void onPositiveClick(View view) {
  146. pd = new ProgressDialog(SplashActivity.this);
  147. pd.setTitle("Downloading");
  148. pd.setCanceledOnTouchOutside(false);
  149. pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  150. downFile("http://iospan.ilove.ren:8092/apk/" + getString(R.string.app_name) + ".apk");
  151. }
  152. });
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * 下载apk
  159. */
  160. public void downFile(final String url) {
  161. pd.show();
  162. OkHttpUtils.get()
  163. .url(url)
  164. .build()
  165. .execute(new FileCallBack(getExternalFilesDir(null).getPath() + File.separator, UPDATE_SERVERAPK) {
  166. @Override
  167. public void inProgress(float progress, long total, int id) {
  168. super.inProgress(progress, total, id);
  169. pd.setProgress((int) (progress * 100));
  170. }
  171. @Override
  172. public void onError(Call call, Exception e, int id) {
  173. pd.cancel();
  174. }
  175. @Override
  176. public void onResponse(File response, int id) {
  177. pd.cancel();
  178. update();
  179. }
  180. });
  181. }
  182. /**
  183. * 安装应用
  184. */
  185. public void update() {
  186. Intent intent = new Intent(Intent.ACTION_VIEW);
  187. intent.setDataAndType(Uri.fromFile(new File(getExternalFilesDir(null).getPath() + File.separator, UPDATE_SERVERAPK)),
  188. "application/vnd.android.package-archive");
  189. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  190. startActivity(intent);
  191. }
  192. private void login() {
  193. OkHttpUtils.post()
  194. .url(IUrlConstant.URL_LOGIN)
  195. .addHeader("token", PlatformInfoXml.getToken())
  196. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  197. .addParams("pushToken", PlatformInfoXml.getPushToken())
  198. .addParams("account", UserInfoXml.getUsername())
  199. .addParams("password", UserInfoXml.getPassword())
  200. .build()
  201. .execute(new LoginCallBack());
  202. }
  203. private class LoginCallBack extends Callback<LoginEntity> {
  204. @Override
  205. public LoginEntity parseNetworkResponse(Response response, int id) throws Exception {
  206. String resultJson = response.body().string();
  207. if (!TextUtils.isEmpty(resultJson)) {
  208. return JSON.parseObject(resultJson, LoginEntity.class);
  209. }
  210. return null;
  211. }
  212. @Override
  213. public void onError(Call call, Exception e, int id) {
  214. if(!isFinish){
  215. isFinish=true;
  216. Util.gotoActivity(SplashActivity.this, LoginActivity.class, true);
  217. finish();
  218. }
  219. }
  220. @Override
  221. public void onResponse(final LoginEntity response, int id) {
  222. if(!isFinish){
  223. isFinish=true;
  224. if (response != null) {
  225. if (!TextUtils.isEmpty(response.getMsg())) {
  226. ToastUtil.showShortToast(SplashActivity.this, response.getMsg());
  227. }
  228. String isSucceed = response.getIsSucceed();
  229. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  230. PlatformInfoXml.setToken(response.getToken());
  231. UserInfoXml.setUserInfo(response.getUserEnglish());
  232. // 设置页眉和互推开关
  233. UserInfoXml.setIsShowHeadMsg(response.getIsShowHeadNotice());
  234. UserInfoXml.setIsShowHeadMsgNew(response.getIsShowHeadNotice2());
  235. UserInfoXml.setIsShowRecommendUser(response.getIsShowCommendUser());
  236. UserInfoXml.setSearchInterceptor(response.getSearchInterceptor());
  237. // appsflyer登录
  238. AppsflyerUtils.login();
  239. CommonRequestUtil.initParamsDict(new CommonRequestUtil.OnCommonListener() {
  240. @Override
  241. public void onSuccess() {
  242. if (response.getIsShowUploadImg() != null && response.getIsShowUploadImg().equals("1")) {
  243. Util.gotoActivity(SplashActivity.this, UploadPhotoActivity.class, true);
  244. } else {
  245. if(UserInfoXml.getGender().equals("1") && response.getIsShowUploadAudioInformation() != null && response.getIsShowUploadAudioInformation().equals("1")){
  246. // Util.gotoActivity(SplashActivity.this,UploadAudioActivity.class,true);
  247. Intent intent = new Intent(SplashActivity.this, UploadAudioActivity.class);
  248. startActivity(intent);
  249. finish();
  250. overridePendingTransition(R.anim.login_anim_in, R.anim.login_anim_out);
  251. }else {
  252. Util.gotoActivity(SplashActivity.this, MainActivity.class, true);
  253. }
  254. }
  255. }
  256. @Override
  257. public void onFail() {
  258. if (response.getIsShowUploadImg() != null && response.getIsShowUploadImg().equals("1")) {
  259. Util.gotoActivity(SplashActivity.this, UploadPhotoActivity.class, true);
  260. } else {
  261. Util.gotoActivity(SplashActivity.this, MainActivity.class, true);
  262. }
  263. }
  264. });
  265. } else {
  266. Util.gotoActivity(SplashActivity.this, LoginActivity.class, true);
  267. }
  268. } else {
  269. Util.gotoActivity(SplashActivity.this, LoginActivity.class, true);
  270. }
  271. }
  272. }
  273. }
  274. private void getPlatformInfo() {
  275. PlatformInfo platformInfo = new PlatformInfo();
  276. platformInfo.setW(String.valueOf(DensityUtil.getScreenWidth(SplashActivity.this)));
  277. platformInfo.setH(String.valueOf(DensityUtil.getScreenHeight(SplashActivity.this)));
  278. platformInfo.setVersion(String.valueOf(VersionInfoUtil.getVersionCode(getApplicationContext())));
  279. platformInfo.setPhonetype(Build.MODEL);
  280. platformInfo.setPid(getAndroidId());
  281. platformInfo.setImsi(getAndroidId());
  282. platformInfo.setProduct(IConfigConstant.PRODUCT_ID);// 产品号
  283. platformInfo.setNetType(getNetType());
  284. platformInfo.setMobileIP(getMobileIP());
  285. platformInfo.setRelease(TimeUtil.getCurrentTime());
  286. platformInfo.setPlatform("2");
  287. platformInfo.setSystemVersion(Build.VERSION.RELEASE);
  288. if (TextUtils.isEmpty(PlatformInfoXml.getCountry())) {
  289. platformInfo.setCountry("America");
  290. platformInfo.setLanguage("English");
  291. }else{
  292. platformInfo.setLanguage(CountryFidUtils.getLanguageMap().get(PlatformInfoXml.getCountry()));
  293. }
  294. if (TextUtils.isEmpty(PlatformInfoXml.getFid())) {
  295. platformInfo.setFid(IConfigConstant.F_ID);
  296. }
  297. // 保存platform信息
  298. PlatformInfoXml.setPlatfromInfo(platformInfo);
  299. }
  300. private String getAndroidId() {
  301. return Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
  302. }
  303. private String getNetType() {
  304. String type = "";
  305. ConnectivityManager cm = (ConnectivityManager) SplashActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
  306. NetworkInfo info = cm.getActiveNetworkInfo();
  307. if (info == null) {
  308. type = "0";
  309. } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
  310. type = "2";
  311. } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
  312. String extraInfo = info.getExtraInfo();
  313. if (extraInfo.equals("cmwap")) {
  314. type = "3";
  315. } else if (extraInfo.equals("cmnet")) {
  316. type = "4";
  317. } else if (extraInfo.equals("ctnet")) {
  318. type = "5";
  319. } else if (extraInfo.equals("ctwap")) {
  320. type = "6";
  321. } else if (extraInfo.equals("3gwap")) {
  322. type = "7";
  323. } else if (extraInfo.equals("3gnet")) {
  324. type = "8";
  325. } else if (extraInfo.equals("uniwap")) {
  326. type = "9";
  327. } else if (extraInfo.equals("uninet")) {
  328. type = "10";
  329. }
  330. }
  331. return type;
  332. }
  333. private String getMobileIP() {
  334. try {
  335. for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
  336. NetworkInterface intf = en.nextElement();
  337. for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
  338. InetAddress inetAddress = enumIpAddr.nextElement();
  339. if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
  340. return inetAddress.getHostAddress().toString();
  341. }
  342. }
  343. }
  344. } catch (Exception e) {
  345. e.printStackTrace();
  346. }
  347. return "";
  348. }
  349. /**
  350. * 请求定位信息
  351. */
  352. private void requestLocation() {
  353. mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  354. if (checkGPS()) {// GPS打开
  355. if (ContextCompat.checkSelfPermission(SplashActivity.this,
  356. Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
  357. // 请求android.permission.ACCESS_FINE_LOCATION
  358. ActivityCompat.requestPermissions(SplashActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_CODE_ACCESS_FINE_LOCATION);
  359. } else {
  360. if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {// Network定位
  361. mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 5, mLocationListener);
  362. } else if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {// GPS定位
  363. uploadLocation(mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER));
  364. }
  365. /**添加代码**/
  366. new Handler().postDelayed(new Runnable() {
  367. @Override
  368. public void run() {
  369. if (UserInfoXml.getUsername() != null && UserInfoXml.getPassword() != null) {
  370. checkRefresh();
  371. } else if (getCountry != null) {
  372. if (getCountry.contains(" ")) {
  373. getCountry = getCountry.replaceAll(" ", "");
  374. }
  375. boolean getLocation=false;
  376. for(int i=0;i<Cheeses.EN_GOUNTRY.length;i++){
  377. if(Cheeses.EN_GOUNTRY[i].equals(getCountry)){
  378. getLocation=true;
  379. skipRegisterActivity(POSITION_SUCCESS);
  380. }
  381. }
  382. if(!getLocation){
  383. skipRegisterActivity(POSITION_FAIL);
  384. }
  385. } else {
  386. skipRegisterActivity(POSITION_FAIL);
  387. }
  388. }
  389. }, 3000);
  390. /**添加代码**/
  391. // 。。。。。
  392. }
  393. } else {// GPS关闭
  394. // 跳转到设置页,打开定位开关
  395. DialogUtil.showDoubleBtnDialog(getSupportFragmentManager(), null, getString(R.string.near_fail), getString(R.string.dialog_set), null, true, new OnDoubleDialogClickListener() {
  396. @Override
  397. public void onPositiveClick(View view) {
  398. Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  399. startActivityForResult(intent, 0);
  400. // requestLocation();
  401. new Handler().postDelayed(new Runnable() {
  402. @Override
  403. public void run() {
  404. requestLocation();
  405. }
  406. }, 3000);
  407. }
  408. @Override
  409. public void onNegativeClick(View view) {
  410. /**添加代码**/
  411. new Handler().postDelayed(new Runnable() {
  412. @Override
  413. public void run() {
  414. if (UserInfoXml.getUsername() != null && UserInfoXml.getPassword() != null) {
  415. checkRefresh();
  416. } else {
  417. // Util.gotoActivity(SplashActivity.this, RegisterActivity.class, true);
  418. skipRegisterActivity(CLOSE_POSITION);
  419. // Intent intent = new Intent(SplashActivity.this, RegisterActivity.class);
  420. // Bundle bundle = new Bundle();
  421. // bundle.putString("locationCode", "3");
  422. // intent.putExtras(bundle);
  423. // finish();
  424. // startActivity(intent);
  425. }
  426. }
  427. }, 3000);
  428. /**添加代码**/
  429. }
  430. });
  431. }
  432. }
  433. private LocationListener mLocationListener = new LocationListener() {
  434. @Override
  435. public void onLocationChanged(Location location) {
  436. if(!removeLocation){
  437. uploadLocation(location);
  438. }
  439. }
  440. @Override
  441. public void onStatusChanged(String provider, int status, Bundle extras) {
  442. }
  443. @Override
  444. public void onProviderEnabled(String provider) {
  445. }
  446. @Override
  447. public void onProviderDisabled(String provider) {
  448. }
  449. };
  450. @Override
  451. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  452. if (requestCode == PERMISSION_CODE_ACCESS_FINE_LOCATION) {
  453. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {// 申请权限成功
  454. if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {// Network定位
  455. mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 5, mLocationListener);
  456. uploadLocation(mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER));
  457. } else if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {// GPS定位
  458. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  459. // TODO: Consider calling
  460. // ActivityCompat#requestPermissions
  461. // here to request the missing permissions, and then overriding
  462. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  463. // int[] grantResults)
  464. // to handle the case where the user grants the permission. See the documentation
  465. // for ActivityCompat#requestPermissions for more details.
  466. return;
  467. }
  468. uploadLocation(mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER));
  469. }
  470. /**添加代码**/
  471. new Handler().postDelayed(new Runnable() {
  472. @Override
  473. public void run() {
  474. if (UserInfoXml.getUsername() != null && UserInfoXml.getPassword() != null) {
  475. checkRefresh();
  476. } else if (getCountry != null) {
  477. if (getCountry.contains(" ")) {
  478. getCountry = getCountry.replaceAll(" ", "");
  479. }
  480. boolean getLocation=false;
  481. for(int i=0;i<Cheeses.EN_GOUNTRY.length;i++){
  482. if(Cheeses.EN_GOUNTRY[i].equals(getCountry)){
  483. getLocation=true;
  484. skipRegisterActivity(POSITION_SUCCESS);
  485. }
  486. }
  487. if(!getLocation){
  488. skipRegisterActivity(POSITION_FAIL);
  489. }
  490. // Intent intent = new Intent(SplashActivity.this, RegisterActivity.class);
  491. // Bundle bundle = new Bundle();
  492. // bundle.putString("getCountry", getCountry);
  493. // bundle.putString("locationCode", "1");
  494. // intent.putExtras(bundle);
  495. // finish();
  496. // startActivity(intent);
  497. // Util.gotoActivity(SplashActivity.this, RegisterActivity.class,true,"getCountry",getCountry);
  498. } else {
  499. skipRegisterActivity(POSITION_FAIL);
  500. // Util.gotoActivity(SplashActivity.this, RegisterActivity.class, true);
  501. // Intent intent = new Intent(SplashActivity.this, RegisterActivity.class);
  502. // Bundle bundle = new Bundle();
  503. // bundle.putString("locationCode", "2");
  504. // intent.putExtras(bundle);
  505. // finish();
  506. // startActivity(intent);
  507. }
  508. }
  509. }, 5000);
  510. /**添加代码**/
  511. } else {
  512. /**添加代码**/
  513. new Handler().postDelayed(new Runnable() {
  514. @Override
  515. public void run() {
  516. if (UserInfoXml.getUsername() != null && UserInfoXml.getPassword() != null) {
  517. checkRefresh();
  518. } else {
  519. // Util.gotoActivity(SplashActivity.this, RegisterActivity.class, true);
  520. skipRegisterActivity(CLOSE_POSITION);
  521. // Intent intent = new Intent(SplashActivity.this, RegisterActivity.class);
  522. // Bundle bundle = new Bundle();
  523. // bundle.putString("locationCode", "3");
  524. // intent.putExtras(bundle);
  525. // finish();
  526. // startActivity(intent);
  527. }
  528. }
  529. }, 3000);
  530. /**添加代码**/
  531. }
  532. }
  533. }
  534. /**跳转到注册界面**/
  535. private void skipRegisterActivity(String locationCode) {
  536. if (!isFinish) {
  537. isFinish=true;
  538. Intent intent = new Intent(SplashActivity.this, RegisterActivity.class);
  539. Bundle bundle = new Bundle();
  540. if (!TextUtils.isEmpty(getCountry)) {
  541. bundle.putString("getCountry", getCountry);
  542. }
  543. if (!TextUtils.isEmpty(locationCode)) {
  544. bundle.putString("locationCode", locationCode);
  545. }
  546. intent.putExtras(bundle);
  547. finish();
  548. startActivity(intent);
  549. overridePendingTransition(R.anim.login_anim_in, R.anim.login_anim_out);
  550. }
  551. }
  552. /**
  553. * 上传位置信息
  554. *
  555. * @param location 定位成功后的信息
  556. */
  557. private void uploadLocation(Location location) {
  558. if (location != null) {
  559. LocationInfo locationInfo = new LocationInfo();
  560. locationInfo.setAddrStr("");
  561. locationInfo.setProvince("");
  562. locationInfo.setCity("");
  563. locationInfo.setCityCode("");
  564. locationInfo.setDistrict("");
  565. locationInfo.setStreet("");
  566. locationInfo.setStreetNumber("");
  567. //英国
  568. // String getLatitude ="52.2729944";
  569. // String getLongitude ="-0.8755515";
  570. // 印度
  571. // String getLatitude ="28.61345942";
  572. // String getLongitude ="77.20092773";
  573. String getLatitude = String.valueOf(location.getLatitude());
  574. String getLongitude = String.valueOf(location.getLongitude());
  575. locationInfo.setLatitude(getLatitude);
  576. locationInfo.setLongitude(getLongitude);
  577. SharedPreferenceUtil.setStringValue(SplashActivity.this,"LOCATION","getLatitude",getLatitude);
  578. SharedPreferenceUtil.setStringValue(SplashActivity.this,"LOCATION","getLongitude",getLongitude);
  579. Map<String, LocationInfo> map = new HashMap<String, LocationInfo>();
  580. map.put("locationInfo", locationInfo);
  581. // 根据经纬度获取国家和地区
  582. if(isfirstGetLoaction){
  583. isfirstGetLoaction=false;
  584. getAddress(getLatitude, getLongitude,"English");
  585. }
  586. }
  587. }
  588. // 获取城市
  589. private void getAddress(String getLatitude, String getLongitude, final String language) {
  590. // 本地
  591. String url = "http://ditu.google.cn/maps/api/geocode/json?latlng="+ getLatitude + "," + getLongitude + "&&sensor=false&&language=";
  592. // 线上
  593. // String url = "http://ditu.google.com/maps/api/geocode/json?latlng="+ lat + "," + lng + "&&sensor=false&&language=";
  594. //获取国际化参数
  595. String localInfo = "en_US";
  596. url += localInfo; //拼接url地址
  597. String charset = "UTF-8";
  598. OkHttpUtils.get()
  599. .url(url)
  600. .build()
  601. .execute(new StringCallback() {
  602. @Override
  603. public void onError(Call call, Exception e, int id) {
  604. Log.v("Exception",e.toString());
  605. }
  606. @Override
  607. public void onResponse(String response, int id) {
  608. if (null != response) {
  609. JSONObject json = JSON.parseObject(response);// 转换为json数据
  610. JSONArray jsonArray = json.getJSONArray("results");
  611. Map<String, String> map = null;
  612. if (null != jsonArray && jsonArray.size() > 0) {// 增加验证
  613. JSONObject objResult = (JSONObject) jsonArray.get(0);
  614. JSONArray addressComponents = objResult.getJSONArray("address_components");
  615. //解析结果
  616. getByGoogleMap(addressComponents, objResult, language);
  617. }
  618. }
  619. }
  620. });
  621. }
  622. private void getByGoogleMap(JSONArray addressComponents, JSONObject objResult, String language) {
  623. String countryResult="";
  624. String province="";
  625. String city="";
  626. if(addressComponents!=null&&addressComponents.size()>0){
  627. for(int i=0;i<addressComponents.size();i++){
  628. JSONObject obj = (JSONObject) addressComponents
  629. .get(i);
  630. JSONArray types = obj.getJSONArray("types");
  631. String type = (String)types.get(0);
  632. //获取目标解析单位的key
  633. String proviceKey ="administrative_area_level_1";
  634. if (type.equals("country")){ //国家
  635. countryResult = obj.getString("long_name");
  636. }
  637. //获取目标解析单位的key
  638. proviceKey = getGoogleProviceL1Key(countryResult);
  639. if(type.equals(proviceKey)){
  640. province = obj.getString("long_name");// 州
  641. }else if(type.equals("administrative_area_level_2")){
  642. city = obj.getString("long_name");// 城市
  643. }else if (type.equals("country")){ //国家
  644. countryResult = obj.getString("long_name");
  645. }
  646. }
  647. // if(StringUtils.isBlank(province)){ //不是一级城市 取二级城市
  648. if(TextUtils.isEmpty(province)){ //不是一级城市 取二级城市
  649. province = city;
  650. }
  651. }
  652. String formatted_address = objResult.getString("formatted_address");
  653. if (!TextUtils.isEmpty(province) && !province.equals(UserInfoXml.getProvinceName())) {
  654. UserInfoXml.setProvinceName(province);
  655. }
  656. SharedPreferenceUtil.setStringValue(SplashActivity.this,"GET_PROVINCE","province",province);
  657. if(!TextUtils.isEmpty(countryResult)){
  658. getCountry = countryResult;
  659. if (UserInfoXml.getUsername() != null && UserInfoXml.getPassword() != null) {
  660. checkRefresh();
  661. } else if (getCountry != null) {
  662. if (getCountry.contains(" ")) {
  663. getCountry = getCountry.replaceAll(" ", "");
  664. }
  665. boolean getLocation=false;
  666. for(int i=0;i<Cheeses.EN_GOUNTRY.length;i++){
  667. if(Cheeses.EN_GOUNTRY[i].equals(getCountry)){
  668. getLocation=true;
  669. skipRegisterActivity(POSITION_SUCCESS);
  670. }
  671. }
  672. if(!getLocation){
  673. skipRegisterActivity(POSITION_FAIL);
  674. }
  675. }
  676. }
  677. }
  678. public static String getGoogleProviceL1Key(String country) {
  679. String proviceL1Key = "administrative_area_level_1";
  680. if("UnitedKingdom".equals(country) || "United Kingdom".equals(country)){
  681. proviceL1Key = "administrative_area_level_2";
  682. }
  683. return proviceL1Key;
  684. }
  685. @Override
  686. protected void onDestroy() {
  687. super.onDestroy();
  688. removeLocation=true;
  689. }
  690. }