百度lbs的使用

官方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 ;
    		      }
    	}); 
    	
	}
}


 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值