体温登记app——进度7
今天把定位功能重新写了一下,用经纬度表示,没有调用百度地图
首先是需要有相应的权限
AndroidManifest.xml文件:
1 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 2 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 3 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 4 <uses-permission android:name="android.permission.INTERNET" />
MainActivity.java
1 //地点 2 private EditText mLocal; 3 public static final int LOCATION_CODE=301; 4 private LocationManager locationManager; 5 private String locationProvider=null; 6 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 //地点 12 mLocal=findViewById(R.id.local); 13 mLocal.setOnTouchListener(new View.OnTouchListener() { 14 @Override 15 public boolean onTouch(View v, MotionEvent event) { 16 if (event.getAction() == MotionEvent.ACTION_DOWN) { 17 getLocation(); 18 return true; 19 } 20 return false; 21 } 22 }); 23 } 24 25 private void getLocation(){ 26 locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE); 27 //获取所有可用的位置提供器 28 List<String> providers=locationManager.getProviders(true); 29 if(providers.contains(LocationManager.NETWORK_PROVIDER)){ 30 //如果是Network 31 locationProvider=locationManager.NETWORK_PROVIDER; 32 } 33 if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){ 34 //获取权限(如果没有开启权限,会弹出对话框,询问是否开启权限) 35 if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED|| ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){ 36 //请求权限 37 ActivityCompat.requestPermissions(this,new String[]{ 38 Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION 39 },LOCATION_CODE); 40 }else{ 41 //监测地理位置变化 42 locationManager.requestLocationUpdates(locationProvider,3000,1,locationListener); 43 Location location=locationManager.getLastKnownLocation(locationProvider); 44 if(location!=null){ 45 //输入经纬度 46 mLocal.setText("经度:"+location.getLongitude() + ",纬度:" + location.getLatitude() + ""); 47 } 48 } 49 }else{ 50 //监视地理位置变化 51 locationManager.requestLocationUpdates(locationProvider,3000,1,locationListener); 52 Location location=locationManager.getLastKnownLocation(locationProvider); 53 if(location!=null){ 54 //不为空,显示地理位置经纬度 55 mLocal.setText("经度:"+location.getLongitude() + ",纬度:" + location.getLatitude() + ""); 56 } 57 } 58 } 59 public LocationListener locationListener = new LocationListener() { 60 // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 61 @Override 62 public void onStatusChanged(String provider, int status, Bundle extras) { 63 } 64 // Provider被enable时触发此函数,比如GPS被打开 65 @Override 66 public void onProviderEnabled(String provider) { 67 } 68 // Provider被disable时触发此函数,比如GPS被关闭 69 @Override 70 public void onProviderDisabled(String provider) { 71 } 72 //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 73 @Override 74 public void onLocationChanged(Location location) { 75 if (location != null) { 76 //不为空,显示地理位置经纬度 77 mLocal.setText("经度:"+location.getLongitude() + ",纬度:" + location.getLatitude() + ""); 78 getAddress(location.getLongitude(),location.getLatitude()); 79 } 80 } 81 }; 82 83 @Override 84 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){ 85 switch (requestCode) { 86 case LOCATION_CODE: 87 if(grantResults.length > 0 && grantResults[0] == getPackageManager().PERMISSION_GRANTED 88 && grantResults[1] == PackageManager.PERMISSION_GRANTED) { 89 Toast.makeText(this, "申请权限", Toast.LENGTH_LONG).show(); 90 try { 91 List<String> providers = locationManager.getProviders(true); 92 if (providers.contains(LocationManager.NETWORK_PROVIDER)) { 93 //如果是Network 94 locationProvider = LocationManager.NETWORK_PROVIDER; 95 }else if (providers.contains(LocationManager.GPS_PROVIDER)) { 96 //如果是GPS 97 locationProvider = LocationManager.GPS_PROVIDER; 98 } 99 //监视地理位置变化 100 locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener); 101 Location location = locationManager.getLastKnownLocation(locationProvider); 102 if (location != null) { 103 //不为空,显示地理位置经纬度 104 mLocal.setText("经度:" + location.getLongitude() + ",纬度:" + location.getLatitude() + ""); 105 } 106 }catch (SecurityException e){ 107 e.printStackTrace(); 108 } 109 } else { 110 Toast.makeText(this, "缺少权限", Toast.LENGTH_LONG).show(); 111 finish(); 112 } 113 break; 114 } 115 } 116 117 @Override 118 protected void onDestroy() { 119 super.onDestroy(); 120 locationManager.removeUpdates(locationListener); 121 }
activity_main.xml
1 <EditText 2 android:id="@+id/local" 3 android:hint="地点" 4 android:textColor="#000000" 5 android:textSize="16sp" 6 android:layout_width="match_parent" 7 android:layout_height="50dp" 8 android:padding="5dp" 9 android:layout_below="@id/time" 10 android:layout_marginTop="20dp" 11 android:background="#FDDEDE" 12 android:clickable="true" 13 />
效果图如下:
明天开始建立数据库