{"id":10006229,"date":"2022-08-17T12:36:09","date_gmt":"2022-08-17T04:36:09","guid":{"rendered":"https:\/\/ityet.com\/?p=10006229"},"modified":"2022-08-17T12:36:09","modified_gmt":"2022-08-17T04:36:09","slug":"app%e5%88%87%e6%8d%a2%e5%88%b0%e5%90%8e%e5%8f%b0%e5%90%8e%e5%a6%82%e4%bd%95%e4%bf%9d%e6%8c%81%e6%8c%81%e7%bb%ad%e5%ae%9a%e4%bd%8d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006229","title":{"rendered":"App\u5207\u6362\u5230\u540e\u53f0\u540e\u5982\u4f55\u4fdd\u6301\u6301\u7eed\u5b9a\u4f4d\uff1f"},"content":{"rendered":"<p>\u4e3a\u4e86\u4fdd\u62a4\u7528\u6237\u9690\u79c1\uff0c\u5927\u591a\u6570\u5e94\u7528\u53ea\u4f1a\u5728\u524d\u53f0\u8fd0\u884c\u65f6\u83b7\u53d6\u7528\u6237\u4f4d\u7f6e\uff0c\u5f53\u5e94\u7528\u5728\u540e\u53f0\u8fd0\u884c\u65f6\uff0c\u5b9a\u4f4d\u529f\u80fd\u4f1a\u88ab\u7981\u6b62\u3002\u8fd9\u5c31\u5bfc\u81f4APP\u5728\u540e\u53f0\u6216\u8005\u9501\u5c4f\u65f6\u65e0\u6cd5\u6b63\u5e38\u8bb0\u5f55GPS\u8f68\u8ff9\uff0c\u8fd9\u5bf9\u6253\u8f66\u3001\u5171\u4eab\u51fa\u884c\u3001\u8dd1\u6b65\u7b49\u9700\u8981\u5b9e\u65f6\u8bb0\u5f55\u7528\u6237\u8f68\u8ff9\u7684\u5e94\u7528\u5f71\u54cd\u975e\u5e38\u5927\uff0c\u751a\u81f3\u5f71\u54cd\u4e86\u5e94\u7528\u6838\u5fc3\u529f\u80fd\u7684\u4f7f\u7528\u4f53\u9a8c\u3002\u90a3\u5bf9\u4e8e\u8fd9\u4e9b\u5e94\u7528\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u5982\u4f55\u5728\u7528\u6237\u4e3b\u52a8\u6388\u6743\u4f4d\u7f6e\u4fe1\u606f\u540e\uff0c\u8ba9\u5e94\u7528\u5728\u540e\u53f0\u8fd0\u884c\u65f6\u957f\u65f6\u95f4\u4fdd\u6301\u6301\u7eed\u5b9a\u4f4d\u5462\uff1f<\/p>\n<p>HMS Core<a href=\"https:\/\/link.segmentfault.com\/?enc=dTMrXYNAZ%2FFZsPMRTmebhQ%3D%3D.iX8cu0ZqyIzy%2BKUyARpxl2GCsluBU2UzgpJUgM5B8ALO0p5s%2FGRry%2FcYxC6zK%2BSOEv06IAM2adVvEHctQhLSxIAaGzKZNWTaVK9Kl81mMic%3D\" rel=\"nofollow\">\u5b9a\u4f4d\u670d\u52a1<\/a>\u63d0\u4f9b\u540e\u53f0\u6301\u7eed\u5b9a\u4f4d\u7684\u80fd\u529b\uff0c\u5728\u83b7\u53d6\u7528\u6237\u4e3b\u52a8\u6388\u6743\u7684\u60c5\u51b5\u4e0b\u53ef\u6301\u4e45\u8bb0\u5f55\u4f4d\u7f6e\u4fe1\u606f\uff0c\u9002\u7528\u4e8e\u8bb0\u8f68\u8ff9\u5f55\u573a\u666f\u3002<\/p>\n<h3>\u4e00\u3001\u878d\u5408\u5b9a\u4f4d-\u540e\u53f0\u5b9a\u4f4d\u5b9e\u73b0\u65b9\u6cd5<\/h3>\n<ol>\n<li>\u5e94\u7528\u8fd0\u884c\u8bbe\u5907\u4e3a\u975e\u534e\u4e3a\u624b\u673a<\/li>\n<li>\u4f7f\u7528LocationCallback\u5f00\u542f\u5b9a\u4f4d\u4e4b\u540e\uff0c\u5f53\u5e94\u7528\u5207\u5230\u540e\u53f0\u4e4b\u540e\uff0c\u5b9a\u4f4d\u5c06\u4f1a\u5f88\u5feb\u505c\u6b62\u3002<\/li>\n<li>\u4e3a\u4e86\u8ba9\u5e94\u7528\u5207\u5230\u540e\u53f0\u4e4b\u540e\uff0c\u5b9a\u4f4d\u80fd\u529b\u4f9d\u65e7\u6709\u6548\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528enableBackgroundLocation\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u524d\u53f0\u670d\u52a1\uff0c\u7528\u4ee5\u63d0\u9ad8\u5e94\u7528\u5728\u540e\u53f0\u7684\u4f4d\u7f6e\u66f4\u65b0\u9891\u7387\u3002<\/li>\n<li>\u540e\u53f0\u5b9a\u4f4d\u672c\u8eab\u4e0d\u5177\u5907\u5b9a\u4f4d\u80fd\u529b\uff0c\u540e\u53f0\u5b9a\u4f4d\u9700\u8981\u548cLocationCallback\u5f00\u542f\u7684\u5b9a\u4f4d\u4e00\u8d77\u4f7f\u7528\u3002\u5b9a\u4f4d\u83b7\u53d6\u7684\u6570\u636e\u9700\u8981\u5728 LocationCallback\u5bf9\u8c61\u4e2d\u7684onLocationResult(LocationResult locationResult) \u65b9\u6cd5\u4e2d\u83b7\u53d6\u3002<\/li>\n<\/ol>\n<h3>\u4e8c\u3001\u6ce8\u610f\u4e8b\u9879\uff1a<\/h3>\n<ol>\n<li>\u652f\u6301\u7684\u8bbe\u5907\u4e3a\u975e\u534e\u4e3a\u624b\u673a<\/li>\n<li>\u5e94\u7528\u9700\u8981\u83b7\u5f97\u5b9a\u4f4d\u6743\u9650\uff0c\u4e14\u5fc5\u987b\u4e3a\u201c\u59cb\u7ec8\u5141\u8bb8\u201d<\/li>\n<li>\u5e94\u7528\u4e0d\u53ef\u88ab\u8bbe\u5907\u4e2d\u7684\u7701\u7535\u7cbe\u7075\u7b49\u63a7\u7535\u5e94\u7528\u51bb\u7ed3\uff0c\u4ee5vivo\u624b\u673a\u4e3a\u4f8b\uff1a\u6253\u5f00i\u7ba1\u5bb6-\u540e\u53f0\u8017\u7535\u7ba1\u7406-\u627e\u5230\u5e94\u7528-\u628a\u667a\u80fd\u63a7\u7535\u6539\u6210\u5141\u8bb8\u540e\u53f0\u9ad8\u8017\u7535\u3002<\/li>\n<\/ol>\n<h3>\u4e09\u3001\u6d4b\u8bd5Demo\u65f6\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/h3>\n<ol>\n<li>\u6d4b\u8bd5\u65f6\u8bbe\u5907\u6700\u597d\u4e0d\u8981\u662f\u5145\u7535\u72b6\u6001\uff0c\u5145\u7535\u72b6\u6001\u4e0b\u5e94\u7528\u53ef\u80fd\u4e0d\u4f1a\u88ab\u63a7\u7535\u3002<\/li>\n<li>\u53ef\u4ee5\u901a\u8fc7\u72b6\u6001\u680f\u662f\u5426\u6709 \u5b9a\u4f4d\u56fe\u8868\u5224\u65ad \u8bbe\u5907\u5f53\u524d\u662f\u5426\u5728\u8fdb\u884c\u5b9a\u4f4d\u3002\u4ee5vivo\u624b\u673a\u4e3a\u4f8b\uff1avivo\u624b\u673a\u5b9a\u4f4d\u5f00\u542f\u65f6\u72b6\u6001\u680f\u4f1a\u5c55\u793a\u4e00\u4e2a\u5b9a\u4f4d\u56fe\u6807\uff0c\u5982\u679c\u4e0d\u5f00\u542f\u540e\u53f0\u5b9a\u4f4d\u7684\u8bdd\u5e94\u7528\u5207\u540e\u53f0 \u5b9a\u4f4d\u56fe\u6807\u4f1a\u6d88\u5931\u3002\u5f00\u542f\u540e\u53f0\u5b9a\u4f4d\u80fd\u529b\u4e4b\u540e\uff0c\u5e94\u7528\u5207\u540e\u53f0\u5b9a\u4f4d\u56fe\u6807\u8fd8\u662f\u5b58\u5728\u7684\u3002<\/li>\n<\/ol>\n<h3>\u56db\u3001\u5b9e\u73b0\u540e\u53f0\u5b9a\u4f4d\u529f\u80fd\u96c6\u6210\u6b65\u9aa4<\/h3>\n<ol>\n<li>\u5728AndroidManifest.xml\u4e2d\u6dfb\u52a0\u540e\u53f0\u5b9a\u4f4d\u670d\u52a1<\/li>\n<\/ol>\n<pre><code>&lt;service\n    android:name=&quot;com.huawei.location.service.BackGroundService&quot;\n    android:foregroundServiceType=&quot;location&quot; \/&gt;<\/code><\/pre>\n<ol start=\"2\">\n<li>\uff08\u53ef\u9009\uff09\u5728Android 9\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u4e3a\u4fdd\u8bc1\u540e\u53f0\u5b9a\u4f4d\u6743\u9650\u7684\u6b63\u5e38\u4f7f\u7528\uff0c\u9700\u8981\u5728\u201cAndroidManifest.xml\u201d\u6587\u4ef6\u4e2d\u914d\u7f6eFOREGROUND_SERVICE\u6743\u9650\uff1a<\/li>\n<\/ol>\n<pre><code>&lt;uses-permission android:name=&quot;android.permission.FOREGROUND_SERVICE&quot; \/&gt;<\/code><\/pre>\n<ol start=\"3\">\n<li>\u521b\u5efaNotification\u5bf9\u8c61<\/li>\n<\/ol>\n<pre><code>public class NotificationUtil {\n    public static final int NOTIFICATION_ID = 1;\n\n    public static Notification getNotification(Context context) {\n        Notification.Builder builder;\n        Notification notification;\n        if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {\n            NotificationManager notificationManager =\n                (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);\n            String channelId = context.getPackageName();\n            NotificationChannel notificationChannel =\n                new NotificationChannel(channelId, &quot;LOCATION&quot;, NotificationManager.IMPORTANCE_LOW);\n            notificationManager.createNotificationChannel(notificationChannel);\n            builder = new Notification.Builder(context, channelId);\n        } else {\n            builder = new Notification.Builder(context);\n        }\n        builder.setSmallIcon(R.drawable.ic_launcher)\n            .setContentTitle(&quot;Location SDK&quot;)\n            .setContentText(&quot;Running in the background &quot;)\n            .setWhen(System.currentTimeMillis());\n        if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN) {\n            notification = builder.build();\n        } else {\n            notification = builder.getNotification();\n        }\n        return notification;\n    }\n}<\/code><\/pre>\n<ol start=\"4\">\n<li>\u521d\u59cb\u5316FusedLocationProviderClient\u5bf9\u8c61<\/li>\n<\/ol>\n<pre><code>FusedLocationProviderClient mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);<\/code><\/pre>\n<ol start=\"5\">\n<li>\u5f00\u542f\u540e\u53f0\u5b9a\u4f4d<\/li>\n<\/ol>\n<pre><code>private void enableBackgroundLocation() {\n    mFusedLocationProviderClient\n            .enableBackgroundLocation(NotificationUtil.NOTIFICATION_ID, NotificationUtil.getNotification(this))\n            .addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {\n                @Override\n                public void onSuccess(Void aVoid) {\n                    LocationLog.i(TAG, &quot;enableBackgroundLocation onSuccess&quot;);\n                }\n            })\n            .addOnFailureListener(new OnFailureListener() {\n                @Override\n                public void onFailure(Exception e) {\n                    LocationLog.e(TAG, &quot;enableBackgroundLocation onFailure:&quot; + e.getMessage());\n                }\n            });\n}<\/code><\/pre>\n<p><strong>\u4e86\u89e3\u66f4\u591a\u8be6\u60c5&gt;&gt;<\/strong><\/p>\n<p>\u8bbf\u95ee<a href=\"https:\/\/link.segmentfault.com\/?enc=JaBYt%2FX7g5cn4F%2BfwqaamA%3D%3D.d0%2FT0kyuq2Zlj%2Bqru5jGFjZUBYIC%2FlwfpE61lcEigcPuVKZybg64%2BnXLM6z8VV1tfW2mWjln4oGqoCfpC3QbNA%3D%3D\" rel=\"nofollow\">\u534e\u4e3a\u5f00\u53d1\u8005\u8054\u76df\u5b98\u7f51<\/a>  <br \/>\u83b7\u53d6<a href=\"https:\/\/link.segmentfault.com\/?enc=bWgI924hq29%2BRMVk5TVvGg%3D%3D.WeZ37m%2BNXyl42vC3RkN4DG0blOyWSMVOgn%2Bvk76uYscfQ%2BsTafqgf%2B9c0kJk1RNJ2ANQEMy0oxCa5WOR94rDxq4Fzgb9PgdX91ImGzUegk4%3D\" rel=\"nofollow\">\u5f00\u53d1\u6307\u5bfc\u6587\u6863<\/a>  <br \/>\u534e\u4e3a\u79fb\u52a8\u670d\u52a1\u5f00\u6e90\u4ed3\u5e93\u5730\u5740\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=JF%2B2Htsa0MmtmbqUOjhZsw%3D%3D.qCxWsUb94tKCyAqRU8TQf5fwC9Sr0XviuSnDtB3bPpI%3D\" rel=\"nofollow\">GitHub<\/a>\u3001<a href=\"https:\/\/link.segmentfault.com\/?enc=3OH7VLKRJgXtDeZaWEk9zA%3D%3D.g4GsLsEnbvGtZY8WvNuXgS4rWisNXEHRbS0AkQhlMGk%3D\" rel=\"nofollow\">Gitee<\/a><\/p>\n<p><strong>\u5173\u6ce8\u6211\u4eec\uff0c\u7b2c\u4e00\u65f6\u95f4\u4e86\u89e3 HMS Core \u6700\u65b0\u6280\u672f\u8d44\u8baf~<\/strong><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042331352\">https:\/\/segmentfault.com\/a\/1190000042331352<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4e86\u4fdd\u62a4\u7528\u6237\u9690\u79c1\uff0c\u5927\u591a\u6570\u5e94\u7528\u53ea\u4f1a\u5728\u524d\u53f0\u8fd0\u884c\u65f6\u83b7\u53d6\u7528\u6237\u4f4d\u7f6e\uff0c\u5f53\u5e94\u7528\u5728\u540e\u53f0\u8fd0\u884c\u65f6\uff0c\u5b9a\u4f4d\u529f\u80fd\u4f1a\u88ab\u7981\u6b62\u3002\u8fd9\u5c31\u5bfc\u81f4APP [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006229","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006229"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006229\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}