From 306263e4c47416bfac0b15b6b4254f7fbb73f994 Mon Sep 17 00:00:00 2001 From: CYZ <2782394648@qq.com> Date: Thu, 20 Oct 2022 09:31:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../8c55c3ccc257e5907959013f99656e4c8ec3903e | 2 + .../ad72331a1bed265bb9c0fe838faa74dbf69fce32 | 102 +++++++++++++++ .../.idea/sonarlint/issuestore/index.pb | 6 +- src/Notes1/.idea/vcs.xml | 6 + src/Notes1/app/src/main/AndroidManifest.xml | 2 + .../net/micode/notes/ui/NoteEditActivity.java | 123 ++++++++++++++++++ 6 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 src/Notes1/.idea/sonarlint/issuestore/8/c/8c55c3ccc257e5907959013f99656e4c8ec3903e create mode 100644 src/Notes1/.idea/sonarlint/issuestore/a/d/ad72331a1bed265bb9c0fe838faa74dbf69fce32 create mode 100644 src/Notes1/.idea/vcs.xml diff --git a/src/Notes1/.idea/sonarlint/issuestore/8/c/8c55c3ccc257e5907959013f99656e4c8ec3903e b/src/Notes1/.idea/sonarlint/issuestore/8/c/8c55c3ccc257e5907959013f99656e4c8ec3903e new file mode 100644 index 0000000..8a51d89 --- /dev/null +++ b/src/Notes1/.idea/sonarlint/issuestore/8/c/8c55c3ccc257e5907959013f99656e4c8ec3903e @@ -0,0 +1,2 @@ + +K xml:S55948"1Implement permissions on this exported component.( \ No newline at end of file diff --git a/src/Notes1/.idea/sonarlint/issuestore/a/d/ad72331a1bed265bb9c0fe838faa74dbf69fce32 b/src/Notes1/.idea/sonarlint/issuestore/a/d/ad72331a1bed265bb9c0fe838faa74dbf69fce32 new file mode 100644 index 0000000..9390065 --- /dev/null +++ b/src/Notes1/.idea/sonarlint/issuestore/a/d/ad72331a1bed265bb9c0fe838faa74dbf69fce32 @@ -0,0 +1,102 @@ + +c java:S112"FDefine and throw a dedicated exception instead of using a generic one.(8ώ0 +w +java:S2293"YReplace the type specification in this constructor call with the diamond operator ("<>").(艥8Ў0 +k +java:S1192"NDefine a constant instead of duplicating this literal "if_create.txt" 3 times.(̬8Ҏ0 +s java:S117"QRename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(8Ԏ0 +n java:S117"QRename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(8Ԏ0 +s java:S117"QRename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(8Վ0 +s java:S117"QRename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ϯ8Վ0 +s java:S117"QRename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(8Վ0 +> +java:S1604"(Make this anonymous inner class a lambda(̺V +? +java:S1604"(Make this anonymous inner class a lambda(ά +? +java:S1604"(Make this anonymous inner class a lambda(ܴ +D +java:S1604"(Make this anonymous inner class a lambda( +F +java:S1604"(Make this anonymous inner class a lambda(8֎0 +F +java:S1604"(Make this anonymous inner class a lambda(۱8֎0 +? +java:S1604"(Make this anonymous inner class a lambda(ά +K +java:S1604"(Make this anonymous inner class a lambda(8׎0 +F +java:S1604"(Make this anonymous inner class a lambda(8׎0 +n +java:S1301"KReplace this "switch" statement by "if" statements to increase readability.(ד8؎0 +l +java:S2095"NUse try-with-resources or close this "FileOutputStream" in a "finally" clause.(80 +U +java:S2589"7Remove this expression which always evaluates to "true"(ą80 +o java:S116w"ZRename this field "if_first_create" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(ݥ÷ +t +java:S1450w"^Remove the "if_first_create" field and declare it as a local variable in the relevant methods.(ݥ÷ +7 +java:S1116e"Remove this empty statement.( +a +java:S1124"EReorder the modifiers to comply with the Java Language Specification.( +[ +java:S1124"EReorder the modifiers to comply with the Java Language Specification.(Ѿk +n +java:S3776"RRefactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.( +2 +java:S3626"Remove this redundant jump.(݅ +2 +java:S3626"Remove this redundant jump.(݅ +2 +java:S3626"Remove this redundant jump.(݅ +N +java:S1135"2Complete the task associated to this TODO comment.(̐ +F +java:S2093"*Change this "try" to a try-with-resources.( +o +java:S1450"XRemove the "mMoveMenu" field and declare it as a local variable in the relevant methods.( +n +java:S3252"RUse static access with "android.widget.AbsListView" for "MultiChoiceModeListener".( +N +java:S1135"2Complete the task associated to this TODO comment.( +N +java:S1135"2Complete the task associated to this TODO comment.( +M +java:S1874"1Remove this use of "getHeight"; it is deprecated.( +8 +java:S1116"Remove this empty statement.( +p +java:S3776"RRefactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.(8Ꮑ0 +F java:S108")Either remove or fill this block of code.(ʥ8⏁0 +o +java:S3776"RRefactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.(#8菁0 +N +java:S1135"2Complete the task associated to this TODO comment.( +e +java:S1126"BReplace this if-then-else statement by a single method invocation.(玒80 +N +java:S1135"2Complete the task associated to this TODO comment.( +l +java:S1871"IThis case's code block is the same as the block for the case on line 683.(80 +p java:S100"NRename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(80 +F +java:S2093"*Change this "try" to a try-with-resources.( +k java:S100"NRename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.(80 +^ java:S125" + + + + + \ No newline at end of file diff --git a/src/Notes1/app/src/main/AndroidManifest.xml b/src/Notes1/app/src/main/AndroidManifest.xml index 40cb406..59be98f 100644 --- a/src/Notes1/app/src/main/AndroidManifest.xml +++ b/src/Notes1/app/src/main/AndroidManifest.xml @@ -25,6 +25,8 @@ + + diff --git a/src/Notes1/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java b/src/Notes1/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java index 8753d3d..852e6f5 100644 --- a/src/Notes1/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java +++ b/src/Notes1/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java @@ -29,7 +29,13 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.graphics.Paint; +import android.location.Address; +import android.location.Geocoder; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; import android.os.Bundle; import android.preference.PreferenceManager; import android.text.Spannable; @@ -75,6 +81,8 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.HashSet; +import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -82,6 +90,9 @@ import java.util.regex.Pattern; public class NoteEditActivity extends Activity implements OnClickListener, NoteSettingChangedListener, OnTextViewChangeListener { + public static final int LOCATION_CODE = 301; + private LocationManager locationManager; + private String locationProvider = null; private class HeadViewHolder { public TextView tvModified; @@ -972,4 +983,116 @@ public class NoteEditActivity extends Activity implements OnClickListener, private void showToast(int resId, int duration) { Toast.makeText(this, resId, duration).show(); } + + private void getLocation(){ + //1.获取位置管理器 + locationManager = (LocationManager) 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(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show(); + return; + } + + Location location = locationManager.getLastKnownLocation(locationProvider); + if (location!=null){ + Toast.makeText(this, location.getLongitude() + " " + + location.getLatitude() + "", Toast.LENGTH_SHORT).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(NoteEditActivity.this, location.getLongitude() + " " + + location.getLatitude() + "", Toast.LENGTH_SHORT).show(); + Log.v("TAG", "监视地理位置变化-经纬度:"+location.getLongitude()+" "+location.getLatitude()); + } + } + }; + + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + switch (requestCode) { + case LOCATION_CODE: + if(grantResults.length > 0 && grantResults[0] == 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){ + Toast.makeText(this, location.getLongitude() + " " + + location.getLatitude() + "", Toast.LENGTH_SHORT).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(this, "缺少权限", Toast.LENGTH_LONG).show(); + finish(); + } + break; + } + } + + //获取地址信息:城市、街道等信息 + private List
getAddress(Location location) { + List
result = null; + try { + if (location != null) { + Geocoder gc = new Geocoder(this, Locale.getDefault()); + result = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1); + Toast.makeText(this, "获取地址信息:"+result.toString(), Toast.LENGTH_LONG).show(); + Log.v("TAG", "获取地址信息:"+result.toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } }