Android简单定位实例
来源:互联网 发布:淘宝店铺主营占比影响 编辑:程序博客网 时间:2024/05/22 03:29
Android简单定位实例
很多应用对定位的要求并不是那么高,也许只是确认一下当前位置大概在城市的那个方向或者临时需要一个当前的经纬度,这时候定位速度应该是第一位的。下面就说说简单定位需求的实现。
步骤
1.启动应用的时候同时启动一个定位服务
2.定位服务获取到定位信息后通过广播告知UI层(activity)
3.UI层处理显示
在下面的的例子中,在获取了当前的位置信息后,便停掉了的定位服务,并没有进行实时定位,当然也可以进行实时定位。
实现代码
定位服务(LocationSvc)代码:
- package com.sc.demo.locate;
- import com.sc.demo.common.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 SunnyCoffee
- * @date 2014-1-19
- * @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);
- //通知Activity
- Intent intent = new Intent();
- intent.setAction(Common.LOCATION_ACTION);
- intent.putExtra(Common.LOCATION, location.toString());
- 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) {
- }
- }
UI处理层代码
- package com.sc.demo;
- import com.sc.demo.common.Common;
- import com.sc.demo.locate.LocationSvc;
- import android.os.Bundle;
- import android.widget.TextView;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- public class MainActivity extends Activity {
- private TextView text;
- private ProgressDialog dialog;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- text = (TextView) findViewById(R.id.text);
- // 注册广播
- IntentFilter filter = new IntentFilter();
- filter.addAction(Common.LOCATION_ACTION);
- this.registerReceiver(new LocationBroadcastReceiver(), filter);
- // 启动服务
- Intent intent = new Intent();
- intent.setClass(this, LocationSvc.class);
- startService(intent);
- // 等待提示
- dialog = new ProgressDialog(this);
- dialog.setMessage("正在定位...");
- dialog.setCancelable(true);
- dialog.show();
- }
- private class LocationBroadcastReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- if (!intent.getAction().equals(Common.LOCATION_ACTION)) return;
- String locationInfo = intent.getStringExtra(Common.LOCATION);
- text.setText(locationInfo);
- dialog.dismiss();
- MainActivity.this.unregisterReceiver(this);// 不需要时注销
- }
- }
- }
公共类
- package com.sc.demo.common;
- /**
- * @author SunnyCoffee
- * @date 2014-1-27
- * @version 1.0
- * @desc desc 公共常量
- *
- */
- public class Common {
- public static final String LOCATION = "location";
- public static final String LOCATION_ACTION = "locationAction";
- }
代码涉及了android的四大组件之三--Activity、Service、BroadcastReceiver 。
Activity启动后启动了Service,Service是用来定位的,在Service定位结束后发送广播到BroadcastReceiver,这里的BroadcastReceiver是作为Activity的内部类,所以并不能过AndroidManifest.xml进行注册,所以采用了方法registerReceiver。而定位就是通过注册监听执行回调获得。
项目源码下载地址http://download.csdn.net/detail/limb99/6888499。项目编码utf-8
注:demo只写了简单的功能,没有做容错。比如,功能实现需要有网络和GPS支持,需要开启“位置服务”(也有的是位置与安全,不同手机不同系统略有区别)。在资源评论里有人说定位不到,我想很可能就是没有开启位置相关的服务。自己做过测试通过,机型为 Sony st27i,Coolpad 5891Q,Galaxy S4 GT-i9500,虚拟机未测试。
更新2014-12-03
自己在测试的时候发现有的机型不能定位,自己手中机型较少所以无法确定具体问题。
其中一个机型Coolpad 5217,Android4.3。现在还没有解决方案,方法慎用。
一些定位思想可以参见http://blog.csdn.net/limb99/article/details/8765584
版权声明:本文为博主原创文章,未经博主允许不得转载。
- Android简单定位实例
- Android简单定位实例
- Android简单定位实例
- Android简单定位实例
- Android GPS定位简单实例
- Android入门之简单GPS定位实例
- 固定定位简单实例
- android简单定位
- Android GPS定位及实例
- android 定位:简单基站定位程序
- google map定位的简单实例
- Android简单基站定位程序
- AGPS定位Android简单实现
- Android 简单基站定位程序
- Android 百度地图简单定位
- Android 百度地图简单定位
- Android 百度定位SDKv4.2及6.0_百度定位实例_安卓定位实例
- Android 百度定位SDKv4.2及6.0_百度定位实例_安卓定位实例
- android定位的实现
- 有关C++中为什么用模板类的原因
- memcache操作
- 数据结构链表之单循环链表
- [LeetCode] Sqrt(x)
- Android简单定位实例
- Linux信号(signal) 机制分析
- 泛型编程和函数指针
- Java 用线程池实现web服务器
- 3.NSNumber,NSValue,NSData,NSDate及简单数据解析
- 7.27文字排版
- zoj3191 Strange Clock(水题,但为何是wrong answer???)
- 4 招教你如何在应用商店脱颖而出
- caffe c++批量抽取特征