Android原生定位
2016-02-19 18:12
357 查看
虽然说现在有很多定位的lib,但有些不想用第三方的我就写了一个原生定位~用什么保证一直定位呢,我采用的是Service + BroadcastReceiver
首先新建一个Service ,代码贴出如下
package com.mobp2p.mobp2psdk.utils;
import com.mobp2p.mobp2psdk.javabean.Common;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
/**
* @author Liwenjie
* @date 2016-1-6
* @version 1.0
* @desc 定位服务
*
*/
public class LocationSvc extends Service implements LocationListener {
private static final String TAG = "LocationSvc";
private LocationManager locationManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
@Override
public void onStart(Intent intent, int startId) {
if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) locationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
else if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) locationManager
.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
this);
//else Toast.makeText(this, "无法定位", Toast.LENGTH_SHORT).show();
}
@Override
public boolean stopService(Intent name) {
return super.stopService(name);
}
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "Get the current position \n" + location + "|" + location.getLatitude() + "|" + location.getLongitude());
//通知Activity
Intent intent = new Intent();
intent.setAction(Common.LOCATION_ACTION);
intent.putExtra(Common.LATITUDE,location.getLatitude() +"");
intent.putExtra(Common.LONGITUDE,location.getLongitude() +"");
sendBroadcast(intent);
// 如果只是需要定位一次,这里就移除监听,停掉服务。如果要进行实时定位,可以在退出应用或者其他时刻停掉定位服务。
locationManager.removeUpdates(this);
stopSelf();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
写完定位服务后,我们需要用广播接收定位消息,但定位的是经纬度,我们需要转换成地址,那么一并在广播里处理了:
private class LocationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(Common.LOCATION_ACTION))
return;
String latitude = intent.getStringExtra(Common.LATITUDE);
String longitude = intent.getStringExtra(Common.LONGITUDE);
SdkPreferences.setLibs(latitude, longitude);
Map<String, Object> callBackMap = new HashMap<String, Object>();
StringBuffer buffer = new StringBuffer();
callBackMap.put("latlng", buffer.append(latitude).append(",")
.append(longitude).toString());
callBackMap.put("sensor", false);
callBackMap.put("language", "zh-CN");
HttpUtils.doGetAsyn(LoadingActivity.this,
"http://maps.google.cn/maps/api/geocode/json", callBackMap,
new CallBack() {
@Override
public void onRequestComplete(String result) {
// TODO Auto-generated method stub
try {
if (result != null) {
JSONObject jsonObject = new JSONObject(
result);
JSONArray array = jsonObject
.optJSONArray("results");
if (array.length() > 0) {
JSONObject addressObject = array
.optJSONObject(0);
SdkPreferences.setAddress(addressObject
.optString("formatted_address"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
接下来需要做的就是在需要用的地方引用了
// 注册广播
IntentFilter filter = new IntentFilter();
filter.addAction(Common.LOCATION_ACTION);
locationBroadcastReceiver = new LocationBroadcastReceiver();
registerReceiver(locationBroadcastReceiver, filter);
// 启动服务
Intent intent = new Intent();
intent.setClass(this, LocationSvc.class);
startService(intent);
以上就是关键代码了~不是很清晰,见谅。。只为了自己收集积累知识用
首先新建一个Service ,代码贴出如下
package com.mobp2p.mobp2psdk.utils;
import com.mobp2p.mobp2psdk.javabean.Common;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
/**
* @author Liwenjie
* @date 2016-1-6
* @version 1.0
* @desc 定位服务
*
*/
public class LocationSvc extends Service implements LocationListener {
private static final String TAG = "LocationSvc";
private LocationManager locationManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
@Override
public void onStart(Intent intent, int startId) {
if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) locationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
else if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) locationManager
.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
this);
//else Toast.makeText(this, "无法定位", Toast.LENGTH_SHORT).show();
}
@Override
public boolean stopService(Intent name) {
return super.stopService(name);
}
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "Get the current position \n" + location + "|" + location.getLatitude() + "|" + location.getLongitude());
//通知Activity
Intent intent = new Intent();
intent.setAction(Common.LOCATION_ACTION);
intent.putExtra(Common.LATITUDE,location.getLatitude() +"");
intent.putExtra(Common.LONGITUDE,location.getLongitude() +"");
sendBroadcast(intent);
// 如果只是需要定位一次,这里就移除监听,停掉服务。如果要进行实时定位,可以在退出应用或者其他时刻停掉定位服务。
locationManager.removeUpdates(this);
stopSelf();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
写完定位服务后,我们需要用广播接收定位消息,但定位的是经纬度,我们需要转换成地址,那么一并在广播里处理了:
private class LocationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(Common.LOCATION_ACTION))
return;
String latitude = intent.getStringExtra(Common.LATITUDE);
String longitude = intent.getStringExtra(Common.LONGITUDE);
SdkPreferences.setLibs(latitude, longitude);
Map<String, Object> callBackMap = new HashMap<String, Object>();
StringBuffer buffer = new StringBuffer();
callBackMap.put("latlng", buffer.append(latitude).append(",")
.append(longitude).toString());
callBackMap.put("sensor", false);
callBackMap.put("language", "zh-CN");
HttpUtils.doGetAsyn(LoadingActivity.this,
"http://maps.google.cn/maps/api/geocode/json", callBackMap,
new CallBack() {
@Override
public void onRequestComplete(String result) {
// TODO Auto-generated method stub
try {
if (result != null) {
JSONObject jsonObject = new JSONObject(
result);
JSONArray array = jsonObject
.optJSONArray("results");
if (array.length() > 0) {
JSONObject addressObject = array
.optJSONObject(0);
SdkPreferences.setAddress(addressObject
.optString("formatted_address"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
接下来需要做的就是在需要用的地方引用了
// 注册广播
IntentFilter filter = new IntentFilter();
filter.addAction(Common.LOCATION_ACTION);
locationBroadcastReceiver = new LocationBroadcastReceiver();
registerReceiver(locationBroadcastReceiver, filter);
// 启动服务
Intent intent = new Intent();
intent.setClass(this, LocationSvc.class);
startService(intent);
以上就是关键代码了~不是很清晰,见谅。。只为了自己收集积累知识用
相关文章推荐
- 02-19 17:57:05.024: E/AndroidRuntime(27831): java.lang.RuntimeException: Unable to start activity
- android:TableLayout表格布局详解
- android: android中dip、dp、px、sp和屏幕密度
- Android知识结构
- 使用apktool 2.0 反编译和批量打包
- Android studio错误Installation failed since the device possibly has stale dexed jars that don't match
- 圆形头像以及一些常见需求形状自定义ImageView组件
- Android需要学习的控件
- 从零開始学android<TabHost标签组件.二十九.>
- Android Studio Gradle多渠道打包
- Android利用方向传感器获得手机的相对角度实例说明
- 一步一步学ROP之Android ARM 32位篇
- ANDROID_MARS学习笔记_S02_011_ANIMATION_LayoutAnimationController
- android中图片,文字拖动效果
- 改变屏幕后,Activity、Fragment的生命周期变化
- android Animation 动画效果收集
- android屏幕适配建议 (二)
- Android Volley完全解析(四),带你从源码的角度理解Volley
- 阅读《Android 从入门到精通》(27)——选项菜单
- Android 图片压缩实现过程代码