安卓开发的定位API

2017-02-17 17:58
package com.wanggjie.weishi.utils;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;

import com.wanggjie.weishi.conf.Containts;

/**
 * @author 王杰
 * @time 2017/2/14  16:21
 * @desc 手机定位API实现步骤
 */
public class LocationUtils extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //拿到手机定位的管理器
        LocationManager location = (LocationManager)getSystemService(LOCATION_SERVICE);
        //当前最好的定位方式
        Criteria criteria = new Criteria();
         //设置定位的精度
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
         //设置定位耗电的级别
        criteria.setPowerRequirement(Criteria.POWER_HIGH);
        //拿到手机最好的定位方式
        String provider = location.getBestProvider(criteria, true);
        //然后通过管理器去执行定位操作
        location.requestLocationUpdates(provider, 0, 0, new LocationListener() {
             //当位置发生改变的时候调用
            @Override
            public void onLocationChanged(Location location) {
                 //获取到经纬度
                double longitude = location.getLongitude();
                double latitude = location.getLatitude();
                String locationDatas = String.valueOf(longitude)+String.valueOf(latitude);
                String address = SpUtils.getSpStr(getApplicationContext(), Containts.SAFEPHONE);
                SmsManager.getDefault().sendTextMessage(address,null,locationDatas,null,null);
            }
            //当状态发生改变的时候调用
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
            //当可以定位的条件发生改变的时候调用
            @Override
            public void onProviderEnabled(String provider) {
            }
             //当不可以定位的时候调用
            @Override
            public void onProviderDisabled(String provider) {
            }
        });
    }
}