{"id":1,"date":"2020-03-15T19:09:48","date_gmt":"2020-03-15T11:09:48","guid":{"rendered":"http:\/\/localhost\/fly\/?p=1"},"modified":"2023-02-03T09:38:56","modified_gmt":"2023-02-03T01:38:56","slug":"%f0%9f%93%8d%e3%80%90android%e3%80%91%e5%8f%96%e5%be%97%e7%b6%93%e7%b7%af%e5%ba%a6-%e7%8d%b2%e5%8f%96%e6%89%8b%e6%a9%9f%e7%b6%93%e7%b7%af%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/%f0%9f%93%8d%e3%80%90android%e3%80%91%e5%8f%96%e5%be%97%e7%b6%93%e7%b7%af%e5%ba%a6-%e7%8d%b2%e5%8f%96%e6%89%8b%e6%a9%9f%e7%b6%93%e7%b7%af%e5%ba%a6\/","title":{"rendered":"\ud83d\udccd\u3010Android\u3011\u53d6\u5f97\u7d93\u7def\u5ea6 \u7372\u53d6\u624b\u6a5f\u7d93\u7def\u5ea6"},"content":{"rendered":"<h1>\ud83d\udccd\u3010Android\u3011\u53d6\u5f97\u7d93\u7def\u5ea6 \u7372\u53d6\u624b\u6a5f\u7d93\u7def\u5ea6<\/h1>\n<h4>Android \u7372\u53d6\u7d93\u7def\u5ea6\u662f\u6307\u4f7f\u7528 Android \u7cfb\u7d71\u4f86\u53d6\u5f97\u667a\u6167\u578b\u624b\u6a5f\u6216\u5e73\u677f\u96fb\u8166\u7684\u4f4d\u7f6e\u4fe1\u606f\uff0c\u5c24\u5176\u662f\u57fa\u65bc GPS \u7684\u7d93\u7def\u5ea6\u3002\u76ee\u524d\uff0c\u6700\u5168\u9762\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 Google Play \u670d\u52d9\uff0c\u5b83\u80fd\u5920\u63d0\u4f9b\u7cbe\u78ba\u7684\u4f4d\u7f6e\u4fe1\u606f\uff0c\u800c\u4e0d\u9700\u8981\u4efb\u4f55\u5100\u5668\u6216\u984d\u5916\u7684\u88dd\u7f6e\u3002<\/h4>\n<h4>Google Play \u670d\u52d9\u7684 API \u5c07\u5f9e GPS \u63a5\u6536\u7684\u4f4d\u7f6e\u8cc7\u8a0a\uff0c\u4ee5\u53ca\u5176\u4ed6\u4efb\u610f\u7684\u5982 Wi-Fi \u7b49\u7684\u7be9\u9078\u51fa\u4f86\u7684\u8a0a\u865f\uff0c\u5c07\u5176\u6253\u5305\u70ba\u5730\u7406\u5de5\u5177\u5eab (android.location)\uff0c\u53ef\u5354\u52a9\u958b\u767c\u8005\u4f7f\u7528\u3002<\/h4>\n<h4>\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528\u5176 API \u63a2\u7d22\u6bcf\u500b\u4f4d\u7f6e\u7684\u7368\u7279\u8cc7\u8a0a\uff0c\u9019\u500b API \u6703\u63d0\u4f9b\u5305\u62ec\u7d93\u7def\u5ea6\u5750\u6a19\u5728\u5167\u7684\u8a73\u7d30\u8cc7\u6599\uff0c\u5305\u62ec\u4f4d\u7f6e\u6e96\u78ba\u6027\u3001\u65b9\u5411\u3001\u901f\u5ea6\u7b49\u7b49\u3002<\/h4>\n<hr \/>\n<h4>\u6587\u7ae0\u76ee\u9304<\/h4>\n<ol>\n<li><a href=\"#a\">\u5c0e\u5165 Location Library<\/a><\/li>\n<li><a href=\"#b\">AndroidManifest \u589e\u52a0\u6b0a\u9650<\/a><\/li>\n<li><a href=\"#c\">\u5224\u65b7\u662f\u5426\u958b\u555f\u5b9a\u4f4d\u7cfb\u7d71<\/a><\/li>\n<li><a href=\"#d\">\u53d6\u5f97\u7d93\u7def\u5ea6<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h4>1.\u5c0e\u5165 Location Library<\/h4>\n<pre><code class=\"language-Groovy line-numbers\">dependencies {\n   implementation 'com.google.android.gms:play-services-location:18.0.0'\n}\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h4>2.AndroidManifest \u589e\u52a0\u6b0a\u9650<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\"\/&gt;\n&lt;uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\"\/&gt;\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>3.\u5224\u65b7\u662f\u5426\u958b\u555f\u5b9a\u4f4d\u7cfb\u7d71<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">val REQUSET_GPS = 444\n\nval locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager\nval gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)\nval network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)\n\nif (gps || network) {\n    getLocation()\n} else {\n    val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)\n    startActivityForResult(intent, REQUSET_GPS)\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h4>4.\u53d6\u5f97\u7d93\u7def\u5ea6<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">private fun getLocation() {\n    val request = LocationRequest.create()\n    request.apply {\n        interval = 10000\n        fastestInterval = 1000\n        priority = LocationRequest.PRIORITY_HIGH_ACCURACY\n    }\n\n    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {\n        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 50)\n    } else {\n        LocationServices.getFusedLocationProviderClient(this)\n            .requestLocationUpdates(request, object : LocationCallback() {\n                override fun onLocationResult(locationResult: LocationResult?) {\n                    LocationServices.getFusedLocationProviderClient(this@MainActivity)\n                        .removeLocationUpdates(this)\n\n                    if (locationResult != null &amp;&amp; locationResult.locations.size &gt; 0) {\n                        val index = locationResult.locations.size - 1\n                        val latitude = locationResult.locations[index].latitude\n                        val longitude = locationResult.locations[index].longitude\n                        Log.e(\"DATA\", \"<span class=\"katex math inline\">latitude<\/span>longitude\")\n                    }\n                }\n            }, Looper.getMainLooper())\n    }\n}\n<\/code><\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1.\u5c0e\u5165Location Library\u3002<br \/>\n2.AndroidManifest\u589e\u52a0\u6b0a\u9650\u3002<br \/>\n3.\u53d6\u5f97\u6b0a\u9650\u3002<br \/>\n4.\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,15,175],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-kotlin","tag-175"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":35,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":1829,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1\/revisions\/1829"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}