|
|
|
@ -1,13 +1,19 @@ |
|
|
|
|
package com.unionmed.unionmedtv.activity; |
|
|
|
|
|
|
|
|
|
import android.Manifest; |
|
|
|
|
import android.content.BroadcastReceiver; |
|
|
|
|
import android.content.Context; |
|
|
|
|
import android.content.Intent; |
|
|
|
|
import android.content.IntentFilter; |
|
|
|
|
import android.content.pm.PackageManager; |
|
|
|
|
import android.graphics.Paint; |
|
|
|
|
import android.location.Location; |
|
|
|
|
import android.location.LocationListener; |
|
|
|
|
import android.location.LocationManager; |
|
|
|
|
import android.net.ConnectivityManager; |
|
|
|
|
import android.net.NetworkInfo; |
|
|
|
|
import android.net.Uri; |
|
|
|
|
import android.os.Build; |
|
|
|
|
import android.os.Bundle; |
|
|
|
|
import android.os.Handler; |
|
|
|
|
import android.os.Message; |
|
|
|
@ -19,8 +25,11 @@ import android.widget.ImageView; |
|
|
|
|
import android.widget.TextView; |
|
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout; |
|
|
|
|
import androidx.constraintlayout.widget.Group; |
|
|
|
|
import androidx.core.app.ActivityCompat; |
|
|
|
|
import androidx.core.content.ContextCompat; |
|
|
|
|
import androidx.leanback.widget.ArrayObjectAdapter; |
|
|
|
|
import androidx.leanback.widget.FocusHighlight; |
|
|
|
|
import androidx.leanback.widget.FocusHighlightHelper; |
|
|
|
@ -30,6 +39,9 @@ import androidx.leanback.widget.OnChildViewHolderSelectedListener; |
|
|
|
|
import androidx.recyclerview.widget.RecyclerView; |
|
|
|
|
import androidx.viewpager.widget.ViewPager; |
|
|
|
|
|
|
|
|
|
import com.hjq.permissions.OnPermissionCallback; |
|
|
|
|
import com.hjq.permissions.Permission; |
|
|
|
|
import com.hjq.permissions.XXPermissions; |
|
|
|
|
import com.unionmed.unionmedtv.R; |
|
|
|
|
import com.unionmed.unionmedtv.adapter.ContentViewPagerAdapter; |
|
|
|
|
import com.unionmed.unionmedtv.base.BaseActivity; |
|
|
|
@ -198,6 +210,9 @@ public class MainActivity extends BaseActivity implements ContentFragment.OnFrag |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static final int LOCATION_CODE = 301; |
|
|
|
|
private LocationManager locationManager; |
|
|
|
|
private String locationProvider = null; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
|
@ -207,6 +222,36 @@ public class MainActivity extends BaseActivity implements ContentFragment.OnFrag |
|
|
|
|
initData(); |
|
|
|
|
initListener(); |
|
|
|
|
initBroadCast(); |
|
|
|
|
|
|
|
|
|
XXPermissions.with(this) |
|
|
|
|
// 申请单个权限
|
|
|
|
|
.permission(Manifest.permission.WRITE_EXTERNAL_STORAGE, Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION) |
|
|
|
|
// 设置权限请求拦截器(局部设置)
|
|
|
|
|
//.interceptor(new PermissionInterceptor())
|
|
|
|
|
// 设置不触发错误检测机制(局部设置)
|
|
|
|
|
//.unchecked()
|
|
|
|
|
.request(new OnPermissionCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onGranted(@NonNull List<String> permissions, boolean all) { |
|
|
|
|
if (!all) { |
|
|
|
|
// toast("获取部分权限成功,但部分权限未正常授予");
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
getLocation(); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onDenied(@NonNull List<String> 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<String> 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<String> 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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|