HomeActivity.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.ynstkz.shitu.android.activity;
  2. import android.Manifest;
  3. import android.content.Intent;
  4. import android.content.pm.PackageManager;
  5. import android.os.Bundle;
  6. import android.support.v4.app.ActivityCompat;
  7. import android.support.v4.app.Fragment;
  8. import android.support.v4.content.ContextCompat;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.LinearLayout;
  12. import android.widget.RelativeLayout;
  13. import com.amap.api.location.AMapLocation;
  14. import com.amap.api.location.AMapLocationClient;
  15. import com.amap.api.location.AMapLocationClientOption;
  16. import com.amap.api.location.AMapLocationListener;
  17. import com.ynstkz.shitu.android.R;
  18. import com.ynstkz.shitu.android.base.TitleBarActivity;
  19. import com.ynstkz.shitu.android.fragment.ConfirmationDialogFragment;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Date;
  22. import butterknife.Bind;
  23. import butterknife.ButterKnife;
  24. public class HomeActivity extends TitleBarActivity implements AMapLocationListener {
  25. private static final String FRAGMENT_DIALOG = "dialog";
  26. private static final int REQUEST_CAMERA_PERMISSION = 1;
  27. @Bind(R.id.rl_lab_home)
  28. RelativeLayout rlLabHome;
  29. @Bind(R.id.rl_lab_nearby)
  30. RelativeLayout rlLabNearby;
  31. @Bind(R.id.rl_lab_usercenter)
  32. RelativeLayout rlLabUsercenter;
  33. @Bind(R.id.ll_bottom_lab)
  34. LinearLayout llBottomLab;
  35. private Fragment[] mFragments;
  36. private int tabIndex;
  37. //定位
  38. public AMapLocationClient mlocationClient;
  39. //声明mLocationOption对象
  40. public AMapLocationClientOption mLocationOption = null;
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. ButterKnife.bind(this);
  45. initView();
  46. initData();
  47. setListener();
  48. }
  49. @Override
  50. protected int getLayoutId() {
  51. return R.layout.activity_home;
  52. }
  53. private void initView(){
  54. mFragments = new Fragment[3];
  55. mFragments[0] = getSupportFragmentManager().findFragmentById(R.id.fm_home);
  56. mFragments[1] = getSupportFragmentManager().findFragmentById(R.id.fm_nearby);
  57. mFragments[2] = getSupportFragmentManager().findFragmentById(R.id.fm_usercent);
  58. changleLayout(0);
  59. }
  60. private void initData(){
  61. mlocationClient = new AMapLocationClient(getApplicationContext());
  62. //初始化定位参数
  63. mLocationOption = new AMapLocationClientOption();
  64. //设置定位监听
  65. mlocationClient.setLocationListener(this);
  66. //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
  67. mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
  68. //获取一次定位结果:
  69. mLocationOption.setOnceLocation(true);
  70. //设置是否返回地址信息(默认返回地址信息)
  71. mLocationOption.setNeedAddress(true);
  72. //设置定位参数
  73. mlocationClient.setLocationOption(mLocationOption);
  74. }
  75. private void setListener(){
  76. //首页
  77. rlLabHome.setOnClickListener(new View.OnClickListener() {
  78. @Override
  79. public void onClick(View view) {
  80. changleLayout(0);
  81. }
  82. });
  83. //附近
  84. rlLabNearby.setOnClickListener(new View.OnClickListener() {
  85. @Override
  86. public void onClick(View view) {
  87. changleLayout(1);
  88. }
  89. });
  90. //个人中心
  91. rlLabUsercenter.setOnClickListener(new View.OnClickListener() {
  92. @Override
  93. public void onClick(View view) {
  94. if(isLogin()){
  95. changleLayout(2);
  96. } else {
  97. startActivityForResult(new Intent(HomeActivity.this, LoginActivity.class), 10);
  98. }
  99. }
  100. });
  101. }
  102. /**
  103. * 切换fragment
  104. *
  105. * @param tabIndex
  106. */
  107. private void changleLayout(int tabIndex) {
  108. this.tabIndex = tabIndex;
  109. getSupportFragmentManager().beginTransaction().hide(mFragments[0]).hide(mFragments[1])
  110. .hide(mFragments[2]).show(mFragments[tabIndex]).commitAllowingStateLoss();
  111. switch (tabIndex) {
  112. }
  113. }
  114. @Override
  115. public void onLocationChanged(AMapLocation amapLocation) {
  116. if (amapLocation != null) {
  117. if (amapLocation.getErrorCode() == 0) {
  118. //定位成功回调信息,设置相关消息
  119. amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
  120. amapLocation.getLatitude();//获取纬度
  121. amapLocation.getLongitude();//获取经度
  122. amapLocation.getAccuracy();//获取精度信息
  123. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  124. Date date = new Date(amapLocation.getTime());
  125. df.format(date);//定位时间
  126. } else {
  127. //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
  128. Log.e("AmapError", "location Error, ErrCode:"
  129. + amapLocation.getErrorCode() + ", errInfo:"
  130. + amapLocation.getErrorInfo());
  131. }
  132. }
  133. }
  134. @Override
  135. protected void onResume() {
  136. super.onResume();
  137. if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
  138. == PackageManager.PERMISSION_GRANTED) {
  139. //启动定位
  140. mlocationClient.startLocation();
  141. } else if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  142. Manifest.permission.ACCESS_COARSE_LOCATION)){
  143. ConfirmationDialogFragment
  144. .newInstance(R.string.app_name,
  145. new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
  146. REQUEST_CAMERA_PERMISSION,
  147. R.string.app_name)
  148. .show(getSupportFragmentManager(), FRAGMENT_DIALOG);
  149. } else {
  150. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
  151. REQUEST_CAMERA_PERMISSION);
  152. }
  153. }
  154. @Override
  155. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  156. switch (requestCode) {
  157. case 10:
  158. if(resultCode == RESULT_OK) { //登陆成功
  159. changleLayout(2);
  160. }
  161. break;
  162. }
  163. }
  164. }