根据GPS获取手机位置

根据GPS获取手机位置坐标的类
package com.wjy.project.railway.tools;

import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.wjy.project.railway.common.SharePreferUtil;

/**
 * Created by WangJinyong on 2018/3/31.
 * 根据GPS获取手机位置
 */

public class GPSService extends Service {

    // 用到位置服务
    private LocationManager lm;
    private MyLocationListener listener;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("tag", "====进入GPS==");
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);

        // List provider = lm.getAllProviders();
        // for(String l: provider){
        // System.out.println(l);
        // }
        listener = new MyLocationListener();
        // 注册监听位置服务
        // 给位置提供者设置条件
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        // 设置参数细化:
        // criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置为最大精度
        // criteria.setAltitudeRequired(false);//不要求海拔信息
        // criteria.setBearingRequired(false);//不要求方位信息
        // criteria.setCostAllowed(true);//是否允许付费
        // criteria.setPowerRequirement(Criteria.POWER_LOW);//对电量的要求

        String proveder = lm.getBestProvider(criteria, true);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        lm.requestLocationUpdates(proveder, 0, 0, listener);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 取消监听位置服务
        lm.removeUpdates(listener);
        listener = null;
    }

    class MyLocationListener implements LocationListener {

        /**
         * 当位置改变的时候回调
         */

        @Override
        public void onLocationChanged(Location location) {
            String longitude = "j:" + location.getLongitude() + "\n";
            String latitude = "w:" + location.getLatitude() + "\n";
            String accuracy = "a" + location.getAccuracy() + "\n";
            Log.e("tag","longitude="+longitude);
            Log.e("tag","latitude="+latitude);
            Log.e("tag","accuracy="+accuracy);
            SharePreferUtil.putString("longitude",longitude);
            SharePreferUtil.putString("latitude",latitude);

            // 发短信给安全号码

            // 把标准的GPS坐标转换成火星坐标
//   InputStream is;
//   try {
//    is = getAssets().open("axisoffset.dat");
//    ModifyOffset offset = ModifyOffset.getInstance(is);
//    PointDouble double1 = offset.s2c(new PointDouble(location
//      .getLongitude(), location.getLatitude()));
//    longitude ="j:" + offset.X+ "\n";
//    latitude = "w:" +offset.Y+ "\n";
//
//   } catch (IOException e) {
//    // TODO Auto-generated catch block
//    e.printStackTrace();
//   } catch (Exception e) {
//    // TODO Auto-generated catch block
//    e.printStackTrace();
//   }

            SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("lastlocation", longitude + latitude + accuracy);
            editor.commit();

        }

        /**
         * 当状态发生改变的时候回调 开启--关闭 ;关闭--开启
         */
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        /**
         * 某一个位置提供者可以使用了
         */
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        /**
         * 某一个位置提供者不可以使用了
         */
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

    }
}
使用的时候先打开服务
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent i = new Intent(getActivity(),GPSService.class);
        getActivity().startService(i);
    }
使用完成后关闭服务
@Override
    public void onStop() {
        super.onStop();
        Intent i = new Intent(getActivity(),GPSService.class);
        getActivity().stopService(i);
    }

你可能感兴趣的:(Android,知识点小模块例子)