Android获取手机位置代码实现
1、项目Src下创建...service包,然后新建GPSService类
package com.zebra.mobilesafe.service;
import java.io.IOException;
import java.io.InputStream;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
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.util.Log;
public class GPSService extends Service {
private static final String TAG = 'GPSService';
// 用到位置服务
private LocationManager lm;
private MyLocationListener listener;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i(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);
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';
// 发短信给安全号码
// 把标准的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);
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
}
}
}
2、AndroidManiFest.xml添加类
注意这个是service类型,很容易出错,即<service android:name='com.zebra.mobilesafe.service.GPSService'/>
3、添加权限
<uses-permission android:name='android.permission.ACCESS_FINE_LOCATION'/>
<uses-permission android:name='android.permission.ACCESS_COARSE_LOCATION'/>
<uses-permission android:name='android.permission.ACCESS_MOCK_LOCATION'/>
4、根据操作启动服务
Intent i = new Intent(context,GPSService.class);
context.startService(i);
赞 (0)