diff --git a/README-CN.md b/README-CN.md index 8f0f578da8..fb5fc4be26 100644 --- a/README-CN.md +++ b/README-CN.md @@ -251,6 +251,11 @@ showSoftInput : 动态显示软键盘 toggleSoftInput : 切换键盘显示与否状态 ``` +> - **定位相关→[LocationUtils.java][location.java]** + ``` + + ``` + > - **日志相关→[LogUtils.java][log.java]→[Test][log.test]** ``` init : 初始化函数 diff --git a/app/src/main/java/com/blankj/androidutilcode/App.java b/app/src/main/java/com/blankj/androidutilcode/App.java index 3062369cec..1a55efc070 100644 --- a/app/src/main/java/com/blankj/androidutilcode/App.java +++ b/app/src/main/java/com/blankj/androidutilcode/App.java @@ -15,7 +15,7 @@ * author: Blankj * blog : http://blankj.com * time : 2016/10/12 - * desc : + * desc : App * */ public class App extends Application { @@ -29,6 +29,7 @@ public static App getInstance() { @Override public void onCreate() { super.onCreate(); + // 内存泄露检查工具 if (LeakCanary.isInAnalyzerProcess(this)) { // This process is dedicated to LeakCanary for heap analysis. // You should not init your app in this process. diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/HandlerActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/HandlerActivity.java index db0b509d74..0f0e07ecf5 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/HandlerActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/HandlerActivity.java @@ -16,7 +16,7 @@ * author: Blankj * blog : http://blankj.com * time : 2016/9/27 - * desc : Device工具类测试 + * desc : Handler工具类测试 * */ public class HandlerActivity extends Activity diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/ImageActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/ImageActivity.java index 9c2b34f333..f27d03c4f9 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/ImageActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/ImageActivity.java @@ -16,7 +16,7 @@ * author: Blankj * blog : http://blankj.com * time : 2016/9/26 - * desc : 图片工具类测试 + * desc : Image工具类测试 * */ public class ImageActivity extends Activity { diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/KeyboardActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/KeyboardActivity.java index 7f65707634..465a4412ef 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/KeyboardActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/KeyboardActivity.java @@ -2,9 +2,7 @@ import android.app.Activity; import android.content.Context; -import android.inputmethodservice.Keyboard; import android.os.Bundle; -import android.os.IBinder; import android.view.MotionEvent; import android.view.View; import android.view.inputmethod.InputMethodManager; @@ -13,8 +11,15 @@ import com.blankj.androidutilcode.R; import com.blankj.utilcode.utils.KeyboardUtils; -import com.blankj.utilcode.utils.NetworkUtils; +/** + *
+ * author: Blankj + * blog : http://blankj.com + * time : 2016/9/27 + * desc : Keyboard工具类测试 + *+ */ public class KeyboardActivity extends Activity implements View.OnClickListener { diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java index 1cf4befa2d..17d340b813 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java @@ -1,6 +1,8 @@ package com.blankj.androidutilcode.activities; import android.app.Activity; +import android.content.Context; +import android.location.Location; import android.os.Bundle; import android.widget.TextView; @@ -15,18 +17,38 @@ * desc : Location工具类测试 * */ - public class LocationActivity extends Activity { + Context mContext; + TextView tvAboutLocation; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity); - TextView tvAboutLocation = (TextView) findViewById(R.id.tv_about_location); + tvAboutLocation = (TextView) findViewById(R.id.tv_about_location); + mContext = this; + + LocationUtils.getInstance(mContext).init(1000, 0, new LocationUtils.OnLocationChangeListener() { + @Override + public void onLocationChanged(Location location) { + //得到纬度 + double latitude = location.getLatitude(); + //得到经度 + double longitude = location.getLongitude(); + tvAboutLocation.setText("getCountryName:" + LocationUtils.getInstance(mContext).getCountryName(latitude, longitude) + + "\ngetLocality:" + LocationUtils.getInstance(mContext).getLocality(latitude, longitude) + + "\ngetStreet:" + LocationUtils.getInstance(mContext).getStreet(latitude, longitude) + ); + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + + } + }); - LocationUtils.getInstance(this).init(new); - tvAboutLocation.setText(""); } } diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/MainActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/MainActivity.java index be8df90684..f4929baec8 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/MainActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/MainActivity.java @@ -12,7 +12,7 @@ * author: Blankj * blog : http://blankj.com * time : 2016/9/29 - * desc : 吐司工具类测试 + * desc : MainActivity * */ public class MainActivity extends Activity { diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/NetworkActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/NetworkActivity.java index fd77765516..8b02cb4bc6 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/NetworkActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/NetworkActivity.java @@ -8,6 +8,14 @@ import com.blankj.androidutilcode.R; import com.blankj.utilcode.utils.NetworkUtils; +/** + *
+ * author: Blankj + * blog : http://blankj.com + * time : 2016/10/13 + * desc : Network工具类测试 + *+ */ public class NetworkActivity extends Activity implements View.OnClickListener { diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/PhoneActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/PhoneActivity.java index 97101a41b6..460456d41b 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/PhoneActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/PhoneActivity.java @@ -8,6 +8,14 @@ import com.blankj.androidutilcode.R; import com.blankj.utilcode.utils.PhoneUtils; +/** + *
+ * author: Blankj + * blog : http://blankj.com + * time : 2016/10/13 + * desc : Phone工具类测试 + *+ */ public class PhoneActivity extends Activity implements View.OnClickListener { diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/ProcessActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/ProcessActivity.java index 6c6dda372b..7d11b3d924 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/ProcessActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/ProcessActivity.java @@ -8,10 +8,17 @@ import com.blankj.utilcode.utils.ProcessUtils; import com.blankj.utilcode.utils.ToastUtils; +/** + *
+ * author: Blankj + * blog : http://blankj.com + * time : 2016/10/13 + * desc : Process工具类测试 + *+ */ public class ProcessActivity extends Activity implements View.OnClickListener { - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/SDCardActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/SDCardActivity.java index 67d1fce6ef..4e5b93bfc6 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/SDCardActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/SDCardActivity.java @@ -12,7 +12,7 @@ * author: Blankj * blog : http://blankj.com * time : 2016/9/27 - * desc : SD卡工具类测试 + * desc : SDCard工具类测试 * */ public class SDCardActivity extends Activity { diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/ToastActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/ToastActivity.java index 7c556b7ad9..172ef86e5b 100644 --- a/app/src/main/java/com/blankj/androidutilcode/activities/ToastActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/activities/ToastActivity.java @@ -15,7 +15,7 @@ * author: Blankj * blog : http://blankj.com * time : 2016/9/29 - * desc : 吐司测试 + * desc : Toast工具类测试 * */ public class ToastActivity extends Activity diff --git a/update_log.md b/update_log.md index 6ad5d1e305..370869a766 100644 --- a/update_log.md +++ b/update_log.md @@ -84,4 +84,4 @@ #### 16/08/03 修复在onCreate中获取view尺寸的bug,MD5和SHA的Bug修复完成(在此感谢ssyijiu) #### 16/08/02 wifi设置界面bug修复,注释排版还在修改,获取mac地址增加判空,新增QQ群:74721490,欢迎加入,新增隐藏状态栏,注释更加全面,工具类已封装,写的时候真的是一个一个测试过去的,宝宝心里苦 #### 16/08/01 新增获取SD卡路径,手机和设备进行分类,代码bug修改部分,小修排版,正在封装类,新增目录中显示方法名,新增获取当前App版本Code -#### 16/07/31 新增点击屏幕空白区域隐藏软键盘,未能成功增加本页目录跳转功能(不支持) +#### 16/07/31 新增点击屏幕空白区域隐藏软键盘,未能成功增加本页目录跳转功能(不支持) \ No newline at end of file diff --git a/utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java b/utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java index 619b7bf4f6..ac2d155914 100644 --- a/utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java +++ b/utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java @@ -2,19 +2,22 @@ import android.content.Context; import android.content.Intent; -import android.content.MutableContextWrapper; +import android.location.Address; import android.location.Geocoder; -import android.location.GpsStatus; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.LocationProvider; import android.os.Bundle; import android.provider.Settings; +import android.util.Log; -import java.util.HashMap; +import java.io.IOException; +import java.util.List; import java.util.Locale; +import static android.support.v7.widget.StaggeredGridLayoutManager.TAG; + /** *
* author: Blankj @@ -35,6 +38,11 @@ public class LocationUtils { private Context mContext; + /** + * LocationUtils构造函数 + * + * @param context 上下文 + */ private LocationUtils(Context context) { mContext = context; mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); @@ -74,10 +82,12 @@ public void openGpsSettings() { mContext.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } -// http://blog.csdn.net/xiong_it/article/details/46968477 - /** - * 初始化位置信息 + * 初始化 + *使用完记得调用{@link #remove()}
+ *需添加权限 {@code
+ *} 需添加权限 {@code
+ *} 需添加权限 {@code
* * @param minTime 最小刷新时间(单位:毫秒) * @param minDistance 最小刷新距离(单位:米) @@ -87,52 +97,113 @@ public void init(long minTime, long minDistance, OnLocationChangeListener listen if (listener == null) return; Location location; mListener = listener; + if (!mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) && !mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + ToastUtils.showShortToastSafe(mContext, "无法定位,请打开定位服务"); + openGpsSettings(); + } if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { // 通过网络获取定位(省电,但精确度略低) location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { - listener.updateLastLocation(location); + listener.onLocationChanged(location); } mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTime, minDistance, myLocationListener); } else if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { // 通过Gps获取定位(精确度高,但耗电) location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { - listener.updateLastLocation(location); + listener.onLocationChanged(location); } mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, myLocationListener); - } else { - openGpsSettings(); } } + /** + * 根据经纬度获取地理位置 + * + * @param latitude 纬度 + * @param longitude 经度 + * @return {@link Address} + */ + public Address getAddress(double latitude, double longitude) { + Geocoder gc = new Geocoder(mContext, Locale.getDefault()); + try { + return gc.getFromLocation(latitude, longitude, 1).get(0); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 根据经纬度获取国家的名字 + * + * @param latitude 纬度 + * @param longitude 经度 + * @return {@link Address} + */ + public String getCountryName(double latitude, double longitude) { + Address address = getAddress(latitude, longitude); + return address == null ? null : address.getCountryName(); + } + + /** + * 根据经纬度获取所在地 + * + * @param latitude 纬度 + * @param longitude 经度 + * @return {@link Address} + */ + public String getLocality(double latitude, double longitude) { + Address address = getAddress(latitude, longitude); + return address == null ? null : address.getLocality(); + } + + /** + * 根据经纬度获取街道名称 + * + * @param latitude 纬度 + * @param longitude 经度 + * @return {@link Address} + */ + public String getStreet(double latitude, double longitude) { + Address address = getAddress(latitude, longitude); + return address == null ? null : address.getAddressLine(0); + } + private class MyLocationListener implements LocationListener { - - // 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 + /** + * 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 + * + * @param location 坐标 + */ @Override public void onLocationChanged(Location location) { if (mListener != null) { - mListener.updateLocation(location); + mListener.onLocationChanged(location); } } - // Provider的在可用、暂时不可用和无服务三个状态直接切换时触发此函数 + /** + * provider的在可用、暂时不可用和无服务三个状态直接切换时触发此函数 + * + * @param provider 提供者 + * @param status 状态 + * @param extras provider可选包 + */ @Override public void onStatusChanged(String provider, int status, Bundle extras) { if (mListener != null) { - mListener.updateStatus(provider, status, extras); + mListener.onStatusChanged(provider, status, extras); } switch (status) { - //GPS状态为可见时 case LocationProvider.AVAILABLE: LogUtils.d("onStatusChanged", "当前GPS状态为可见状态"); break; - //GPS状态为服务区外时 case LocationProvider.OUT_OF_SERVICE: LogUtils.d("onStatusChanged", "当前GPS状态为服务区外状态"); break; - //GPS状态为暂停服务时 case LocationProvider.TEMPORARILY_UNAVAILABLE: LogUtils.d("onStatusChanged", "当前GPS状态为暂停服务状态"); break; @@ -154,20 +225,35 @@ public void onProviderDisabled(String provider) { } } - // 移除 - public void removeLocationUpdatesListener() { + /** + * 移除 + *} 销毁时一定要移除,否则会内存泄漏
+ */ + public void remove() { if (mLocationManager != null) { mLocationManager.removeUpdates(myLocationListener); mLocationManager = null; + myLocationListener = null; + System.gc(); } } public interface OnLocationChangeListener { - void updateLocation(Location location);//位置信息发生改变 - - void updateStatus(String provider, int status, Bundle extras);//位置状态发生改变 + /** + * 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 + * + * @param location 坐标 + */ + void onLocationChanged(Location location); - void updateLastLocation(Location location); + /** + * provider的在可用、暂时不可用和无服务三个状态直接切换时触发此函数 + * + * @param provider 提供者 + * @param status 状态 + * @param extras provider可选包 + */ + void onStatusChanged(String provider, int status, Bundle extras);//位置状态发生改变 } }