{"id":1025,"date":"2017-04-01T18:34:19","date_gmt":"2017-04-01T10:34:19","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1025"},"modified":"2017-04-01T18:34:19","modified_gmt":"2017-04-01T10:34:19","slug":"android%e5%bc%80%e5%8f%91%e4%b9%8blocation%e5%ae%9a%e4%bd%8d%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/android\/2017\/04\/01\/1834191025.html","title":{"rendered":"Android\u5f00\u53d1\u4e4bLocation\u5b9a\u4f4d\u7684\u57fa\u672c\u4f7f\u7528"},"content":{"rendered":"<p>1\u3001\u57fa\u672cUI\uff0c\u4e0a\u56fe<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2017\/04\/device-2017-04-01-181713.png\" alt=\"\" width=\"300\" height=\"533\" class=\"alignnone size-medium wp-image-1027\" \/> <img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2017\/04\/device-2017-04-01-181643.jpg\" alt=\"\" width=\"300\" height=\"533\" class=\"alignnone size-full wp-image-1031\" \/><\/p>\n<p>2\u3001\u4f7f\u7528\u6b65\u9aa4<br \/>\n2.1\u3001\u57fa\u672c\u89c6\u56fe\u521b\u5efa\u4e00\u4e2a\u6587\u672c\u6846\uff0c\u4e24\u4e2a\u6309\u94ae\uff1b<br \/>\n2.2\u3001\u6309\u94ae\u4e8b\u4ef6\u5206\u522b\u52a0\u8f7d\u6700\u4f73\u5b9a\u4f4d\u65b9\u6848\u4e0e\u5f53\u524d\u4f4d\u7f6e\u663e\u793a\u5230\u6587\u672c\u6846\u4e2d\uff1b<br \/>\n2.3\u3001\u4f7f\u7528Location\u7684\u57fa\u672c\u6761\u4ef6\uff1a<br \/>\n2.3.1\u3001\u5728AndoirdManifest.xml\u4e2d\u7533\u8bf7\u6743\u9650<\/p>\n<pre line=\"1\" lang=\"java\"><uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\" \/>\n<uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\" \/><\/pre>\n<p>2.3.2\u3001\u4f7f\u7528Activity\u7684getSystemService()\u65b9\u6cd5\u83b7\u53d6LocationManager\u5bf9\u8c61\uff0c\u53c2\u6570\u4e3aContext.LOCATION_SERVICE\uff1b<br \/>\n2.3.3\u3001\u4f7f\u7528LocationManager\u7684getAllProviders()\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u652f\u6301\u7684\u6240\u6709\u5b9a\u4f4d\u65b9\u5f0f\uff0c\u4e3b\u8981\u6709GPS\u4e0eNetwork\uff1b<br \/>\n2.3.4\u3001\u521b\u5efaCriteria\u5bf9\u8c61\u53ca\u8bbe\u7f6e\u5404\u79cd\u6761\u4ef6\uff0c\u4f7f\u7528LocationManager\u7684getBestProvider()\u65b9\u6cd5\u53ef\u4ee5\u81ea\u52a8\u7b5b\u9009\u51fa\u5f53\u524d\u6700\u4f73\u5b9a\u4f4d\u65b9\u5f0f\uff1b<br \/>\n2.3.5\u3001LocationManager\u7684requestLocationUpdates()\u65b9\u6cd5\u53ef\u4ee5\u8bf7\u6c42\u5b9a\u4f4d\uff0c\u5b9a\u4f4d\u7ed3\u679c\u5728LocationListener\u76d1\u542c\u5668\u4e2d\u56de\u8c03\uff1b<\/p>\n<p>3\u3001\u53c2\u8003\u4ee3\u7801<!--more--><br \/>\nactivity_main.xml<\/p>\n<pre line=\"1\" lang=\"java\"><?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:orientation=\"vertical\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\"com.yusian.location.MainActivity\">\n\n    <TextView\n        android:id=\"@+id\/tv_message\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Hello World!\"\n        android:textSize=\"18sp\"\n        android:padding=\"10dp\"\/>\n\n    <Button\n        android:id=\"@+id\/btn_best_provider\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"\u5b9a\u4f4d\u6700\u4f73\u65b9\u6848\"\/>\n\n    <Button\n        android:id=\"@+id\/btn_get_location\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"\u83b7\u53d6\u4f4d\u7f6e\"\/>\n\n<\/LinearLayout><\/pre>\n<p>MainActivity<\/p>\n<pre line=\"1\" lang=\"java\">package com.yusian.location;\n\nimport android.Manifest;\nimport android.content.Context;\nimport android.content.pm.PackageManager;\nimport android.location.Criteria;\nimport android.location.Location;\nimport android.location.LocationListener;\nimport android.location.LocationManager;\nimport android.support.v4.app.ActivityCompat;\nimport android.support.v7.app.AppCompatActivity;\nimport android.os.Bundle;\nimport android.util.Log;\nimport android.view.View;\nimport android.widget.TextView;\n\nimport java.util.Iterator;\nimport java.util.List;\n\npublic class MainActivity extends AppCompatActivity implements View.OnClickListener{\n    private final static String TAG = \"SALog\";\n    private LocationManager locationManager = null;\n    private TextView textView = null;\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        textView = (TextView) findViewById(R.id.tv_message);\n        findViewById(R.id.btn_best_provider).setOnClickListener(this);\n        findViewById(R.id.btn_get_location).setOnClickListener(this);\n        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);\n\n    }\n    \/*\u6309\u94ae\u4e8b\u4ef6\u5904\u7406*\/\n    @Override\n    public void onClick(View v) {\n        switch (v.getId()){\n            case R.id.btn_best_provider:{\n                List<String> list = locationManager.getAllProviders();\n                Iterator iterator = list.iterator();\n                StringBuilder builder = new StringBuilder();\n                while (iterator.hasNext()){\n                    String str = (String) iterator.next();\n                    builder.append(str+\"\\n\");\n                }\n                textView.setText(builder.toString());\n            }break;\n            case R.id.btn_get_location:{\n                this.getLocation();\n            }break;\n            default:break;\n        }\n    }\n    \/*\u83b7\u53d6\u5f53\u524d\u5b9a\u4f4d\u4fe1\u606f*\/\n    public void getLocation(){\n        \/\/ \u6743\u9650\u5224\u65ad\n        Boolean b1 = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED;\n        Boolean b2 = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED;\n        if (b1 && b2) {\n            \/\/ TODO: Consider calling\n            \/\/    ActivityCompat#requestPermissions\n            \/\/ here to request the missing permissions, and then overriding\n            \/\/   public void onRequestPermissionsResult(int requestCode, String[] permissions,\n            \/\/                                          int[] grantResults)\n            \/\/ to handle the case where the user grants the permission. See the documentation\n            \/\/ for ActivityCompat#requestPermissions for more details.\n            Log.d(TAG, \"onCreate: \u65e0\u5b9a\u4f4d\u6743\u9650\uff01\");\n        }\n        locationManager.requestLocationUpdates(this.getBestProvider(), 0, 0, new LocationListener() {\n            @Override\n            public void onLocationChanged(Location location) {\n                Log.d(TAG, \"onLocationChanged: \" + location);\n                textView.setText(location.toString());\n            }\n            @Override\n            public void onStatusChanged(String provider, int status, Bundle extras) {\n                Log.d(TAG, \"onStatusChanged: \");\n            }\n            @Override\n            public void onProviderEnabled(String provider) {\n                Log.d(TAG, \"onProviderEnabled: \");\n            }\n            @Override\n            public void onProviderDisabled(String provider) {\n                Log.d(TAG, \"onProviderDisabled: \");\n            }\n        });\n    }\n    \/*\u83b7\u53d6\u6700\u4f73\u5b9a\u4f4d\u65b9\u6848*\/\n    public String getBestProvider(){\n        Criteria criteria = new Criteria();\n        criteria.setAccuracy(Criteria.ACCURACY_FINE);\n        criteria.setBearingRequired(false);\n        criteria.setCostAllowed(false);\n        criteria.setPowerRequirement(Criteria.POWER_LOW);\n        String provider = locationManager.getBestProvider(criteria, true);\n        Log.d(TAG, \"getBestProvider: \"+provider);\n        return provider;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u57fa\u672cUI\uff0c\u4e0a\u56fe 2\u3001\u4f7f\u7528\u6b65\u9aa4 2.1\u3001\u57fa\u672c\u89c6\u56fe\u521b\u5efa\u4e00\u4e2a\u6587\u672c\u6846\uff0c\u4e24\u4e2a\u6309\u94ae\uff1b 2.2\u3001\u6309\u94ae\u4e8b\u4ef6\u5206\u522b\u52a0\u8f7d\u6700\u4f73\u5b9a\u4f4d\u65b9\u6848\u4e0e\u5f53\u524d\u4f4d\u7f6e\u663e\u793a\u5230\u6587\u672c\u6846\u4e2d\uff1b 2.3\u3001\u4f7f\u7528Location\u7684\u57fa\u672c\u6761\u4ef6\uff1a 2.3.1\u3001\u5728AndoirdManifest.xml\u4e2d\u7533\u8bf7\u6743\u9650 2.3.2\u3001\u4f7f\u7528Activity\u7684getSystemService()\u65b9\u6cd5\u83b7\u53d6LocationManager\u5bf9\u8c61\uff0c\u53c2\u6570\u4e3aContext.LOCATION_SERVICE\uff1b 2.3.3\u3001\u4f7f\u7528LocationManager\u7684getAllProviders()\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u652f\u6301\u7684\u6240\u6709\u5b9a\u4f4d\u65b9\u5f0f\uff0c\u4e3b\u8981\u6709GPS\u4e0eNetwork\uff1b 2.3.4\u3001\u521b\u5efaCriteria\u5bf9\u8c61\u53ca\u8bbe\u7f6e\u5404\u79cd\u6761\u4ef6\uff0c\u4f7f\u7528LocationManager\u7684getBestProvider()\u65b9\u6cd5\u53ef\u4ee5\u81ea\u52a8\u7b5b\u9009\u51fa\u5f53\u524d\u6700\u4f73\u5b9a\u4f4d\u65b9\u5f0f\uff1b 2.3.5\u3001LocationManager\u7684requestLocationUpdates()\u65b9\u6cd5\u53ef\u4ee5\u8bf7\u6c42\u5b9a\u4f4d\uff0c\u5b9a\u4f4d\u7ed3\u679c\u5728LocationListener\u76d1\u542c\u5668\u4e2d\u56de\u8c03\uff1b 3\u3001\u53c2\u8003\u4ee3\u7801<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[33],"class_list":["post-1025","post","type-post","status-publish","format-standard","hentry","category-android","tag-33"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1025","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1025"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1025\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}