From c05047e6bac217d733a10906c640cd1e76ba0835 Mon Sep 17 00:00:00 2001 From: linruijn Date: Thu, 25 May 2023 13:36:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E4=B8=8A=E7=9A=84=E7=BB=8F=E7=BA=AC=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 3 + app/src/main/AndroidManifest.xml | 11 ++ .../unionmed/unionmedtv/activity/MainActivity.java | 178 +++++++++++++++++++++ .../unionmedtv/fragment/ContentFragment.java | 161 ++++++++++++++++++- 4 files changed, 350 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 21df5a1..870d97a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -66,4 +66,7 @@ dependencies { implementation 'com.squareup.okhttp3:okhttp:3.12.13' implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.github.getActivity:GsonFactory:6.2' + + // 权限请求框架:https://github.com/getActivity/XXPermissions + implementation 'com.github.getActivity:XXPermissions:16.5' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 00bf821..b87ea0e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,6 +10,10 @@ + + + + @@ -35,8 +39,15 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" + android:usesCleartextTraffic="true" + android:requestLegacyExternalStorage="true" tools:ignore="GoogleAppIndexingWarning" tools:replace="android:allowBackup"> + + + permissions, boolean all) { + if (!all) { +// toast("获取部分权限成功,但部分权限未正常授予"); + return; + } + getLocation(); + + } + + @Override + public void onDenied(@NonNull List permissions, boolean never) { + if (never) { + // 如果是被永久拒绝就跳转到应用权限系统设置页面 + XXPermissions.startPermissionActivity(MainActivity.this, permissions); + } else { + Toast.makeText(MainActivity.this, "位置权限获取失败", Toast.LENGTH_LONG).show(); + } + } + }); } // private boolean isPressUpDownLeftRightBack = false; @@ -249,6 +294,11 @@ public class MainActivity extends BaseActivity implements ContentFragment.OnFrag mThread.interrupt(); } unregisterReceiver(networkChangeReceiver); + + if (locationManager != null) { + // 关闭程序时将监听器移除 + locationManager.removeUpdates(locationListener); + } } private HorizontalGridView mHorizontalGridView;//导航栏Tab @@ -444,6 +494,134 @@ public class MainActivity extends BaseActivity implements ContentFragment.OnFrag } + private void getLocation() { + //1.获取位置管理器 + locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); + + //2.获取位置提供器,GPS或是NetWork + List providers = locationManager.getProviders(true); + + if (providers.contains(LocationManager.NETWORK_PROVIDER)) { + //如果是Network + locationProvider = LocationManager.NETWORK_PROVIDER; + Log.v("TAG", "定位方式Network"); + } else if (providers.contains(LocationManager.GPS_PROVIDER)) { + //如果是GPS + locationProvider = LocationManager.GPS_PROVIDER; + Log.v("TAG", "定位方式GPS"); + } else { + Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_LONG).show(); + return; + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + //获取权限(如果没有开启权限,会弹出对话框,询问是否开启权限) + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + != PackageManager.PERMISSION_GRANTED || + ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) + != PackageManager.PERMISSION_GRANTED) { + //请求权限 + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, + Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_CODE); + } else { + //3.获取上次的位置,一般第一次运行,此值为null + Location location = null; + try { + location = locationManager.getLastKnownLocation(locationProvider); + } catch (Exception e) { + Log.v("TAG", "locationManager.getLastKnownLocation异常了" + e); + } + + if (location != null) { + Toast.makeText(this, location.getLongitude() + " " + + location.getLatitude() + "", Toast.LENGTH_LONG).show(); + Log.v("TAG", "获取上次的位置-经纬度:" + location.getLongitude() + " " + location.getLatitude()); +// TODO 保存经纬度 + + } else { + //监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace + locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener); + } + } + } else { + Location location = locationManager.getLastKnownLocation(locationProvider); + if (location != null) { + Toast.makeText(this, location.getLongitude() + " " + + location.getLatitude() + "", Toast.LENGTH_LONG).show(); + Log.v("TAG", "获取上次的位置-经纬度:" + location.getLongitude() + " " + location.getLatitude()); +// TODO 保存经纬度 + + } else { + //监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace + locationManager.requestLocationUpdates(locationProvider, 5000, 20, locationListener); + } + } + } + + public LocationListener locationListener = new LocationListener() { + // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + } + + // Provider被enable时触发此函数,比如GPS被打开 + @Override + public void onProviderEnabled(String provider) { + } + + // Provider被disable时触发此函数,比如GPS被关闭 + @Override + public void onProviderDisabled(String provider) { + } + + //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 + @Override + public void onLocationChanged(Location location) { + if (location != null) { + //如果位置发生变化,重新显示地理位置经纬度 + Toast.makeText(MainActivity.this, location.getLongitude() + " " + + location.getLatitude() + "", Toast.LENGTH_LONG).show(); + Log.v("TAG", "监视地理位置变化-经纬度:" + location.getLongitude() + " " + location.getLatitude()); + + // TODO 保存经纬度 + } + } + }; + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + switch (requestCode) { + case LOCATION_CODE: + if (grantResults.length > 0 && grantResults[0] == this.getPackageManager().PERMISSION_GRANTED + && grantResults[1] == PackageManager.PERMISSION_GRANTED) { + Toast.makeText(this, "申请定位权限", Toast.LENGTH_LONG).show(); + try { + List providers = locationManager.getProviders(true); + if (providers.contains(LocationManager.NETWORK_PROVIDER)) { + //如果是Network + locationProvider = LocationManager.NETWORK_PROVIDER; + } else if (providers.contains(LocationManager.GPS_PROVIDER)) { + //如果是GPS + locationProvider = LocationManager.GPS_PROVIDER; + } + Location location = locationManager.getLastKnownLocation(locationProvider); + if (location != null) { + // TODO 保存经纬度 + Log.v("TAG", "获取上次的位置-经纬度:" + location.getLongitude() + " " + location.getLatitude()); + } else { + // 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace + locationManager.requestLocationUpdates(locationProvider, 5000, 20, locationListener); + } + + } catch (SecurityException e) { + e.printStackTrace(); + } + } else { + Toast.makeText(this, "缺少定位权限", Toast.LENGTH_LONG).show(); + } + break; + } + } + } diff --git a/app/src/main/java/com/unionmed/unionmedtv/fragment/ContentFragment.java b/app/src/main/java/com/unionmed/unionmedtv/fragment/ContentFragment.java index 3bc29e4..6b75cba 100644 --- a/app/src/main/java/com/unionmed/unionmedtv/fragment/ContentFragment.java +++ b/app/src/main/java/com/unionmed/unionmedtv/fragment/ContentFragment.java @@ -1,8 +1,16 @@ package com.unionmed.unionmedtv.fragment; +import android.Manifest; import android.annotation.SuppressLint; import android.content.Context; +import android.content.pm.PackageManager; +import android.location.Address; +import android.location.Geocoder; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -11,9 +19,12 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentActivity; import androidx.leanback.widget.ArrayObjectAdapter; import androidx.leanback.widget.HeaderItem; @@ -23,6 +34,8 @@ import androidx.leanback.widget.OnChildViewHolderSelectedListener; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; +import com.hjq.http.EasyHttp; +import com.hjq.http.listener.OnHttpListener; import com.unionmed.unionmedtv.R; import com.unionmed.unionmedtv.activity.MainActivity; import com.unionmed.unionmedtv.api.GetNearDoctorApi; @@ -44,10 +57,9 @@ import com.unionmed.unionmedtv.utils.Constants; import com.unionmed.unionmedtv.utils.FontDisplayUtil; import com.unionmed.unionmedtv.utils.LocalJsonResolutionUtil; import com.unionmed.unionmedtv.widgets.TabVerticalGridView; -import com.hjq.http.EasyHttp; -import com.hjq.http.listener.OnHttpListener; import java.util.List; +import java.util.Locale; import me.jessyan.autosize.utils.LogUtils; @@ -190,7 +202,7 @@ public class ContentFragment extends BaseLazyLoadFragment { ItemBridgeAdapter itemBridgeAdapter = new ItemBridgeAdapter(mAdapter); mVerticalGridView.setAdapter(itemBridgeAdapter); - +// getLocation(); } private void initListener() { @@ -578,4 +590,147 @@ public class ContentFragment extends BaseLazyLoadFragment { } } + public static final int LOCATION_CODE = 301; + private LocationManager locationManager; + private String locationProvider = null; + + private void getLocation(){ + //1.获取位置管理器 + locationManager = (LocationManager) mActivity.getSystemService(Context.LOCATION_SERVICE); + + //2.获取位置提供器,GPS或是NetWork + List providers = locationManager.getProviders(true); + +// if (providers.contains(LocationManager.GPS_PROVIDER)) { +// //如果是GPS +// locationProvider = LocationManager.GPS_PROVIDER; +// Log.v("TAG", "定位方式GPS"); +// } else + if (providers.contains(LocationManager.NETWORK_PROVIDER)) { + //如果是Network + locationProvider = LocationManager.NETWORK_PROVIDER; + Log.v("TAG", "定位方式Network"); + }else { + Toast.makeText(mActivity, "没有可用的位置提供器", Toast.LENGTH_LONG).show(); + + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + //获取权限(如果没有开启权限,会弹出对话框,询问是否开启权限) + if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_FINE_LOCATION) + != PackageManager.PERMISSION_GRANTED || + ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_COARSE_LOCATION) + != PackageManager.PERMISSION_GRANTED) { + //请求权限 + ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, + Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_CODE); + } else { + //3.获取上次的位置,一般第一次运行,此值为null + Location location = locationManager.getLastKnownLocation(locationProvider); + if (location!=null){ + Toast.makeText(mActivity, location.getLongitude() + " " + + location.getLatitude() + "",Toast.LENGTH_LONG).show(); + Log.v("TAG", "获取上次的位置-经纬度:"+location.getLongitude()+" "+location.getLatitude()); + getAddress(location); + + }else{ + //监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace + locationManager.requestLocationUpdates(locationProvider, 3000, 1,locationListener); + } + } + } else { + Location location = locationManager.getLastKnownLocation(locationProvider); + if (location!=null){ + Toast.makeText(mActivity, location.getLongitude() + " " + + location.getLatitude() + "", Toast.LENGTH_LONG).show(); + Log.v("TAG", "获取上次的位置-经纬度:"+location.getLongitude()+" "+location.getLatitude()); + getAddress(location); + + }else{ + //监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace + locationManager.requestLocationUpdates(locationProvider, 3000, 1,locationListener); + } + } + } + + public LocationListener locationListener = new LocationListener() { + // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + } + // Provider被enable时触发此函数,比如GPS被打开 + @Override + public void onProviderEnabled(String provider) { + } + // Provider被disable时触发此函数,比如GPS被关闭 + @Override + public void onProviderDisabled(String provider) { + } + //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 + @Override + public void onLocationChanged(Location location) { + if (location != null) { + //如果位置发生变化,重新显示地理位置经纬度 + Toast.makeText(mActivity, location.getLongitude() + " " + + location.getLatitude() + "", Toast.LENGTH_LONG).show(); + Log.v("TAG", "监视地理位置变化-经纬度:"+location.getLongitude()+" "+location.getLatitude()); + } + } + }; + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + switch (requestCode) { + case LOCATION_CODE: + if(grantResults.length > 0 && grantResults[0] == mActivity.getPackageManager().PERMISSION_GRANTED + && grantResults[1] == PackageManager.PERMISSION_GRANTED) { + Toast.makeText(mActivity, "申请权限", Toast.LENGTH_LONG).show(); + try { + List providers = locationManager.getProviders(true); + if (providers.contains(LocationManager.NETWORK_PROVIDER)) { + //如果是Network + locationProvider = LocationManager.NETWORK_PROVIDER; + + }else if (providers.contains(LocationManager.GPS_PROVIDER)) { + //如果是GPS + locationProvider = LocationManager.GPS_PROVIDER; + } + Location location = locationManager.getLastKnownLocation(locationProvider); + if (location!=null){ + Toast.makeText(mActivity, location.getLongitude() + " " + + location.getLatitude() + "", Toast.LENGTH_LONG).show(); + Log.v("TAG", "获取上次的位置-经纬度:"+location.getLongitude()+" "+location.getLatitude()); + }else{ + // 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace + locationManager.requestLocationUpdates(locationProvider, 0, 0,locationListener); + } + + }catch (SecurityException e){ + e.printStackTrace(); + } + } else { + Toast.makeText(mActivity, "缺少权限", Toast.LENGTH_LONG).show(); +// finish(); + } + break; + } + } + + //获取地址信息:城市、街道等信息 + private List
getAddress(Location location) { + List
result = null; + try { + if (location != null) { + Geocoder gc = new Geocoder(mActivity, Locale.getDefault()); + result = gc.getFromLocation(location.getLatitude(), + location.getLongitude(), 1); + Toast.makeText(mActivity, "获取地址信息:"+result.toString(), Toast.LENGTH_LONG).show(); + Log.v("TAG", "获取地址信息:"+result.toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + }