赞
踩
http://download.csdn.net/detail/u013091087/9405004
以下是一个定位获取当前所在城市的Android·Demo,用真机并且联网才能获取,不然就是奔溃报错,退出。
1、首先拿到相关的jar放到项目的libs中
2、在项目的AndroidManifest清单中添加一个定位服务和相关的权限
- <service
- android:name="com.baidu.location.f"
- android:enabled="true"
- android:process=":remote" >
- <intent-filter>
- <action android:name="com.baidu.location.service_v2.2" >
- </action>
- </intent-filter>
- </service>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.READ_CONTACTS" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <uses-permission android:name="android.permission.READ_LOGS" />
- <uses-permission android:name="android.permission.VIBRATE" />
- <uses-permission android:name="android.permission.WAKE_LOCK" />
3、开撸代码
1)、 布局文件activty_main:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
-
- <Button
- android:id="@+id/btn_location"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="定位" />
-
- <TextView
- android:id="@+id/tv_city"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/btn_location"
- android:layout_marginTop="10dp"
- android:gravity="center"
- android:text="@string/hello_world" />
-
- </RelativeLayout>
2)、MyApplication,在AndroidManifest中的application中要注册上去,当打开app时,首先加载该类
该类具体是实现定位功能(发起定位,停止定位,定位相关属性设置,定位成功发送广播到MainActivity中进行处理)
- package com.jiaui.locationdemo;
-
- import android.app.Application;
- import android.content.Intent;
-
- import com.baidu.location.BDLocation;
- import com.baidu.location.BDLocationListener;
- import com.baidu.location.LocationClient;
- import com.baidu.location.LocationClientOption;
-
- public class MyApplication extends Application {
-
- private static LocationClient mLC = null;
-
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- super.onCreate();
- initLocation();
- }
-
- /**
- * 初始化定位的内容
- */
- private void initLocation() {
- mLC = new LocationClient(this);
- // 使用eclipse的SHA1值和该Project的包名去百度地图API那里注册一个key
- mLC.setAK("Aiy12UcER9wwkppqaINQZoA0");
- mLC.registerLocationListener(new MyLocationListener());
- }
-
- /**
- * 发起定位
- */
- public static void requestLocationInfo() {
- setLocationOption();
- if (mLC != null && !mLC.isStarted())
- mLC.start();
- if (mLC != null && mLC.isStarted())
- mLC.requestLocation();
- }
-
- /**
- * 停止定位
- */
- private void stopLocationClient() {
- if (mLC != null && mLC.isStarted())
- mLC.stop();
- }
-
- /**
- * 设置定位相关参数
- */
- private static void setLocationOption() {
- LocationClientOption option = new LocationClientOption();
- option.setOpenGps(true);// 打开GPS
- option.setCoorType("bd09ll");// 设置坐标类型
- option.setServiceName("com.baidu.location.service_v2.9");
- option.setPoiExtraInfo(true);
- option.setAddrType("all");// 详细地址
- option.setPoiNumber(10);
- option.disableCache(true);
- option.setPriority(LocationClientOption.NetWorkFirst);
- option.setPriority(LocationClientOption.GpsFirst); // gps
- mLC.setLocOption(option);
- }
-
- private class MyLocationListener implements BDLocationListener {
-
- public void onReceiveLocation(BDLocation location) {
- if (location == null) {
- sendBroadCast("定位失败");
- return;
- }
- sendBroadCast(location.getCity());
- }
-
- public void onReceivePoi(BDLocation poilocation) {
- if (poilocation == null) {
- sendBroadCast("定位失败");
- return;
- }
- sendBroadCast(poilocation.getCity());
- }
- }
-
- /**
- * 得到结果发送广播
- *
- * @param city
- */
- private void sendBroadCast(String city) {
- stopLocationClient();// 停止定位
- Intent intent = new Intent(MainActivity.LOCATION_BCR);
- intent.putExtra("city", city);
- sendBroadcast(intent);
- }
-
- }
3)、MainActivity,注册广播-初始化组件-监听,操作。
- package com.jiaui.locationdemo;
-
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
-
- public class MainActivity extends Activity {
-
- private Button mLocationBtn;// 定位按钮
- private TextView mCityNameTv;// 显示当前定位成功的城市
-
- public static final String LOCATION_BCR = "location_bcr";
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- registerBroadCastReceiver();
- initView();
- initListen();
-
- }
-
- /**
- * 初始化组件
- */
- private void initView() {
- mLocationBtn = (Button) findViewById(R.id.btn_location);
- mCityNameTv = (TextView) findViewById(R.id.tv_city);
- }
-
- /**
- * 初始化监听
- */
- private void initListen() {
- mLocationBtn.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- mCityNameTv.setText("定位中...");
- // 发起定位
- MyApplication.requestLocationInfo();
- }
- });
- }
-
- /**
- * 初始化广播,重写onReceive,处理接收回来的结果
- */
- private BroadcastReceiver mLocationBcr = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- String city = intent.getStringExtra("city");
- mCityNameTv.setText(city);
- }
- };
-
- /**
- * 注册广播
- */
- private void registerBroadCastReceiver() {
- IntentFilter intentToReceiverFilter = new IntentFilter();
- intentToReceiverFilter.addAction(LOCATION_BCR);
- registerReceiver(mLocationBcr, intentToReceiverFilter);
- }
-
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- unregisterReceiver(mLocationBcr);// 注销广播
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。