本文主要是介绍根据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;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();Log.e("tag", "====进入GPS==");lm = (LocationManager) getSystemService(LOCATION_SERVICE);// List<String> 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);}@Overridepublic void onDestroy() {super.onDestroy();// 取消监听位置服务lm.removeUpdates(listener);listener = null;}class MyLocationListener implements LocationListener {/*** 当位置改变的时候回调*/@Overridepublic 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();}/*** 当状态发生改变的时候回调 开启--关闭 ;关闭--开启*/@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}/*** 某一个位置提供者可以使用了*/@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stub}/*** 某一个位置提供者不可以使用了*/@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stub}}
}
使用的时候先打开服务
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Intent i = new Intent(getActivity(),GPSService.class);getActivity().startService(i);}
使用完成后关闭服务
@Overridepublic void onStop() {super.onStop();Intent i = new Intent(getActivity(),GPSService.class);getActivity().stopService(i);}
这篇关于根据GPS获取手机位置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!