官方api 地址http://developer.baidu.com/map/geosdk-android.htm
使用2.6的jar包时部分机型报错
11-05 10:35:18.321: E/AndroidRuntime(5032): java.lang.SecurityException: Not allowed to bind to service Intent { act=com.baidu.location.service_v2.6 }
更换2.6.c的jar包
我的使用
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import cn.txwx.tool.NetTest;
import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
public class MyLbsService extends Service{
private LocationClient mLocationClient = null;
private int errorcode;
private String str;
private String count;//判断从哪个Activity启动的
private boolean t=false;
public Handler handler;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
handler = new Handler() {
public void handleMessage(android.os.Message msg){
if (msg.what==100) {
stopSelf();//停止服务
}
};
};
count=intent.getStringExtra("num");
boolean can=new NetTest(MyLbsService.this).testConnectivityManager();
//TxwxActivite启动的服务
if("frist".equals(count)){
if(can){//网络连接成功
findlatlnt();//定位
}else{
Toast.makeText(MyLbsService.this, "网络连接不可用!", Toast.LENGTH_LONG).show();
}
}else if("two".equals(count)){//merchantlistActivity启动的服务
if(can){//网络连接成功
findlatlnt();//定位
Message msg = new Message();
msg.what=100;
MerchantListActivity.handler.sendMessage(msg);
}else{
Toast.makeText(MyLbsService.this, "网络连接不可用!", Toast.LENGTH_LONG).show();
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
if (mLocationClient != null && mLocationClient.isStarted()){
mLocationClient.stop();
mLocationClient = null;
}
super.onDestroy();
}
public void findlatlnt(){
mLocationClient = new LocationClient(this);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setCoorType("bd09ll");
option.setPriority(LocationClientOption.NetWorkFirst);
option.setProdName("txwx");
option.setScanSpan(888);
mLocationClient.setLocOption(option);
mLocationClient.start();
mLocationClient.registerLocationListener(new BDLocationListener() {
public void onReceiveLocation(BDLocation location) {
Log.e("location", location+":::::::::::::::::::::::::::::::::::::::::;;;");
if (location == null){
return ;
}
errorcode=location.getLocType();
switch (errorcode) {
case 61:
Toast.makeText(MyLbsService.this, "GPS定位成功", Toast.LENGTH_SHORT).show();
break;
case 62:
Toast.makeText(MyLbsService.this, "定位失败", Toast.LENGTH_SHORT).show();
break;
case 63:
Toast.makeText(MyLbsService.this, "网络异常", Toast.LENGTH_SHORT).show();
break;
case 65:
//定位缓存
Toast.makeText(MyLbsService.this, "定位成功", Toast.LENGTH_SHORT).show();
break;
case 161:
Toast.makeText(MyLbsService.this, "网络定位成功", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(MyLbsService.this, "服务端定位失败", Toast.LENGTH_SHORT).show();
break;
}
str = location.getLatitude()+","+location.getLongitude();
//将经纬度存入本地
SharedPreferences sharedPreferences=MyLbsService.this.getSharedPreferences("lbs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("lbs", str);
//提交修改
editor.commit();
MyLbsService.this.handler.sendEmptyMessage(100);
}
public void onReceivePoi(BDLocation location){
//return ;
}
});
}
}