LocationManager的使用

MainActivity.java

package com.sdutacm.locationservice;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements LocationListener{

    //android中进行地理位置定位主要使用的类是LocationManager类,
    //使用LocationManager的方法就先使用其他服务,只需要调用getSystemService()就能获得他的实例,\
    // 并获得他的引用,当然这里需要传入参数Context.LOCATION_SERVICE
    LocationManager locationManager;
    //当获得Location对象后,直接通过LocationManager调用LastKnownLocation()方法就可以获得Location对象,
    //而Location类正是保存位置信息的类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(locationManager != null){
            checkPermission(new String[]{
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
            });
            //解除监听
            locationManager.removeUpdates(this);
            locationManager =null;
        }
    }
    private void initLocation() {
        checkPermission(new String []{
            Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
        });
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //这里使用GPS位置提供者作为案例
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        //监听位置变化,每个两秒且距离差距为10米时更新位置信息,这助于控制电量
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,10,this);
        if(location != null){
            Log.d("location","1 纬度: "+location.getLatitude()+",经度: "+location.getLongitude());
            getLocation(location);
        }
    }

    private void getLocation(Location location) {
        Log.d("location","123321");
        Geocoder geocoder = new Geocoder(this);
        Log.d("location","123");
        if(geocoder.isPresent()){
            Log.d("location","geocoder is OK");
        }


        try {
            Log.d("location","123456");
            //使用Geocoder获得具体的地址,参数为纬度和经度
            List<Address> addresses = new ArrayList<Address>();
            addresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);

                Log.d("location","as is the "+geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1));

            Log.d("location","123654addresses.get(0) is the "+addresses.get(0));

            Address address = addresses.get(0);
            Log.d("location","12333333");
            Log.d("location","address is the "+address+" addresses.get(0) "+addresses.get(0));
            Log.d("location",address.getAddressLine(1)+address.getFeatureName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onLocationChanged(Location location) {
        //当符合监听条件时,会更新地理位置
        Log.d("location","纬度: "+location.getLatitude()+", 经度"+location.getLatitude());
        getLocation(location);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
    private void checkPermission(String[] permissions) {
        int permission_granted = PackageManager.PERMISSION_GRANTED;
        boolean flag = false;
        for (int i=0;i<permissions.length;i++){
            int checkPermission = ActivityCompat.checkSelfPermission(this,permissions[i]);
            if(permission_granted != checkPermission){
                flag = true;
                break;
            }
        }
        if(flag){
            ActivityCompat.requestPermissions(this,permissions,111);
            return;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 111:
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(this,"获取权限成功!",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this,"获取权限失败",Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }

    }
}
posted @ 2017-08-07 18:47  Philtell  阅读(123)  评论(0编辑  收藏  举报