本文是使用百度的定位SDK V4.2来获取安卓设备的GPS信息,因此配合百度地图使用的时候,可以直接使用坐标信息,而不用进行坐标转换。可以对比上一篇博文《Android实现>>>普通GPS定位<<<并将坐标信息上传到数据库~》来看。

百度的定位没有实现CPU叫醒功能,所以在设备锁屏后会失效,关于这点的解决,经过各种途径的探索,我使用了一个AlarmManager来解决,具体可以看下面的代码,方法不一定很好,还请各位多多指教,有好的解决方式麻烦也告诉我。

另外,关于百度定位SDK V4.2的所有问题,可以参考官方资料:http://developer.baidu.com/map/index.php?title=android-locsdk/guide/v4-2

下面进入正题,惯例,先上包结构图:

>>百度GPS定位_第1张图片" width="229" height="415" style="border:1px solid black;">

对比上一个项目,可以看出libs文件夹里多了一个armeabi文件夹(包含一个 liblocSDK4d.so 文件)和BaiduLBS_Android.jar (都是使用百度定位SDK所需)。
其中,HttpUtil.java 文件没有任何变化,内容不再列出。

首先在AndroidManifest.xml文件添加权限:(仅仅进行定位并上传的话,可能不会全用到,可以自行挑选)

 

然后在application标签里添加:

            android:value="你的AK" />  
和:
       

            android:name="com.example.gpstest.BDGpsService" >
           
               
           


MainActivity:


package com.example.gpstest;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private static final String LOCSTART = "START_LOCATING";
private Button startBtn;
private Button endBtn;
private Button endApp;
private TextView content;
private LocationReceiver lr;
private AlarmManager alarmManager;
private PendingIntent pi;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MainActivity", "********MainActivity onCreate*******");
startBtn = (Button) findViewById(R.id.startServBtn);
endBtn = (Button) findViewById(R.id.endServBtn);
endApp = (Button) findViewById(R.id.endApp);
content = (TextView) findViewById(R.id.content);
alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

startBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("MainActivity", "********MainActivity startBtn onClick*******");
Intent intent = new Intent(LOCSTART);
pi = PendingIntent.getService(getApplicationContext(), 0, intent, 
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pi);
Toast.makeText(getApplicationContext(), "GPS测试开始", Toast.LENGTH_SHORT).show();
}
});
endBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.i("MainActivity", "********MainActivity endBtn onClick*******");
alarmManager.cancel(pi);
Intent intent = new Intent(LOCSTART);
stopService(intent);
Toast.makeText(getApplicationContext(), "GPS测试结束", Toast.LENGTH_SHORT).show();
}
});
endApp.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
android.os.Process.killProcess(android.os.Process.myPid());
}
});
lr = new LocationReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("NEW LOCATION SENT");
registerReceiver(lr, intentFilter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("MainActivity", "********MainActivity onDestroy*******");
unregisterReceiver(lr);
}

class LocationReceiver extends BroadcastReceiver {

String locationMsg = "";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
locationMsg = intent.getStringExtra("newLoca");
content.setText(locationMsg);
}
}
}

BDGpsService:


package com.example.gpstest;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.example.util.BDGpsServiceListener;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class BDGpsService extends Service {

private static final int minTime = 60000;
private LocationClient locationClient;
private BDLocationListener locationListener;
private LocationClientOption lco;

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("BDGpsService", "********BDGpsService onCreate*******");
lco = new LocationClientOption();
lco.setLocationMode(LocationMode.Hight_Accuracy);
lco.setScanSpan(minTime);
lco.setCoorType("bd09ll");
lco.setOpenGps(true);
lco.setIsNeedAddress(true);
locationListener = new BDGpsServiceListener(getApplicationContext());
locationClient = new LocationClient(getApplicationContext());
locationClient.setLocOption(lco);
locationClient.registerLocationListener(locationListener);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("BDGpsService", "********BDGpsService onStartCommand*******");
if (locationClient != null && !locationClient.isStarted()){
locationClient.start();
}
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("BDGpsService", "********BDGpsService onDestroy*******");
if (locationClient != null && locationClient.isStarted()){
locationClient.stop();
}
locationClient.unRegisterLocationListener(locationListener);
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}

BDGpsServiceListener:

package com.example.util;

import java.util.HashMap;
import java.util.Map;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BDGpsServiceListener implements BDLocationListener {

private Context context;

public BDGpsServiceListener(){
super();
}
public BDGpsServiceListener(Context context){
super();
this.context = context;
}

//发送广播,提示更新界面
private void sendToActivity(String str){
Intent intent = new Intent();
intent.putExtra("newLoca", str);
intent.setAction("NEW LOCATION SENT");
context.sendBroadcast(intent);
}
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
Log.i("Listener", "********BDGpsServiceListener onReceiveLocation*******");
if(location == null){return;}
StringBuffer sb = new StringBuffer();
sb.append("经度=").append(location.getLongitude());
sb.append("\n纬度=").append(location.getLatitude());
sb.append("\n时间=").append(location.getTime());
sb.append("\nERR Code=").append(location.getLocType());
if (location.hasRadius()){
sb.append("\n定位精度=").append(location.getRadius());
}
if (location.getLocType() == BDLocation.TypeGpsLocation){
sb.append("\n速度=");
sb.append(location.getSpeed());
sb.append("\n卫星=");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\n位置=").append(location.getAddrStr());
sb.append("\n省=").append(location.getProvince());
sb.append("\n市=").append(location.getCity());
sb.append("\n区县=").append(location.getDistrict());
}

sendToActivity(sb.toString());

try {
Map map = new HashMap();
map.put("longi", location.getLongitude()+"");
map.put("lati", location.getLatitude()+"");
map.put("time", location.getTime());
String url = HttpUtil.BASE_URL+"coords.do?method=addCoords";
HttpUtil.postRequest(url, map);
} catch (Exception e) {
e.printStackTrace();
}
}
}

更多相关文章

  1. Android(安卓)webView加载html代码详解
  2. attrs.xml文件中属性类型format值的格式
  3. Android中Window添加View的底层原理
  4. Android(安卓)MimeType的用途以及所有类型
  5. Android(安卓)接入阿里反馈 (基础版)
  6. Android使用FileObserver对sdcard文件或文件夹监控
  7. android studio 升级到3.0.1 原有项目运行在android 4.4停止运行
  8. Unity 实现Android不锁屏
  9. Android(安卓)Camera子系统之进程/文件View

随机推荐

  1. Android快速开发-选项卡
  2. Android下pm 命令详解
  3. android linux 基础知识总结
  4. Android:java.lang.SecurityException: P
  5. Android按钮事件响应顺序
  6. Android(安卓)P签名机制和系统权限
  7. Android成长之路-Android组件_EditView例
  8. 遇到的Android(安卓)eclipse 问题
  9. android.os.NetworkOnMainThreadExceptio
  10. Android(安卓)SDK开发包国内下载地址