Location.java
package com.example.baiduloctest; import java.util.HashMap; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; import com.baidu.location.*; import android.app.Application; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.TextView; public class Location extends Application { public LocationClient mLocationClient = null; public TextView mTv; private String mData; public Handler hd = null; public MyLocationListenner myListener = new MyLocationListenner(); @Override public void onCreate() { mLocationClient = new LocationClient( this ); mLocationClient.registerLocationListener( myListener ); super.onCreate(); } public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location == null) return ; StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("\nerror code : "); sb.append(location.getLocType()); sb.append("\nlatitude : "); sb.append(location.getLatitude()); sb.append("\nlontitude : "); sb.append(location.getLongitude()); sb.append("\nradius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation){ sb.append("\nspeed : "); sb.append(location.getSpeed()); sb.append("\nsatellite : "); sb.append(location.getSatelliteNumber()); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ sb.append("\naddr : "); sb.append(location.getAddrStr()); } sb.append("\nsdk version : "); sb.append(mLocationClient.getVersion()); sb.append("\nisCellChangeFlag : "); sb.append(location.isCellChangeFlag()); final String url = "http://casic.sinaapp.com/trace.php?ac=add"; final Map<String,String> params = new HashMap<String,String>(); final String longtitude; final String latitude; longtitude = ""+location.getLongitude(); latitude = ""+location.getLatitude(); params.put("longtitude", longtitude); params.put("latitude", latitude); params.put("telephone","18600232563"); new Thread(){ public void run(){ String msgStr=HttpUploadUtil.postWithoutFile(url,params); Bundle b = new Bundle(); try { JSONObject jsonObject = new JSONObject(msgStr); String str = MyConverter.unescape(jsonObject.getString("msg")); b.putString("msg", str); } catch (JSONException e) { b.putString("msg", "转换异常"); e.printStackTrace(); } Message msg = new Message(); msg.what = 1; msg.setData(b); hd.sendMessage(msg); } }.start(); logMsg(sb.toString()); } public void onReceivePoi(BDLocation poiLocation) { if (poiLocation == null){ return ; } StringBuffer sb = new StringBuffer(256); sb.append("Poi time : "); sb.append(poiLocation.getTime()); sb.append("\nerror code : "); sb.append(poiLocation.getLocType()); sb.append("\nlatitude : "); sb.append(poiLocation.getLatitude()); sb.append("\nlontitude : "); sb.append(poiLocation.getLongitude()); sb.append("\nradius : "); sb.append(poiLocation.getRadius()); if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){ sb.append("\naddr : "); sb.append(poiLocation.getAddrStr()); } if(poiLocation.hasPoi()){ sb.append("\nPoi:"); sb.append(poiLocation.getPoi()); }else{ sb.append("noPoi information"); } final String url = "http://casic.sinaapp.com/trace.php?ac=add"; final Map<String,String> params = new HashMap<String,String>(); final String longtitude; final String latitude; longtitude = ""+poiLocation.getLongitude(); latitude = ""+poiLocation.getLatitude(); params.put("longtitude", longtitude); params.put("latitude", latitude); params.put("telephone","18600232563"); new Thread(){ public void run(){ String msgStr=HttpUploadUtil.postWithoutFile(url,params); Bundle b = new Bundle(); try { JSONObject jsonObject = new JSONObject(msgStr); String str = MyConverter.unescape(jsonObject.getString("msg")); b.putString("msg", str); } catch (JSONException e) { b.putString("msg", "转换异常"); e.printStackTrace(); } Message msg = new Message(); msg.what = 1; msg.setData(b); hd.sendMessage(msg); } }.start(); logMsg(sb.toString()); } public void logMsg(String str) { try { mData = str; if ( mTv != null ) mTv.setText(mData); } catch (Exception e) { e.printStackTrace(); } } } }
LocTestActivity.java
package com.example.baiduloctest; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; import android.widget.Toast; import android.annotation.SuppressLint; import android.app.Activity; public class LocTestActivity extends Activity { private LocationClient mLocClient; private TextView mTv = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_loc_test); mLocClient = ((Location)getApplication()).mLocationClient; mTv = (TextView)findViewById(R.id.txtInfo); setLocationOption(); ((Location)getApplication()).mTv = mTv; ((Location)getApplication()).hd = hd; mLocClient.start(); } @SuppressLint("HandlerLeak") Handler hd = new Handler(){ @Override public void handleMessage(Message msg){ super.handleMessage(msg); Bundle b; b = msg.getData(); String msgStr = b.getString("msg"); switch(msg.what){ case 1: b = msg.getData(); msgStr = b.getString("msg"); Toast.makeText(LocTestActivity.this, msgStr, Toast.LENGTH_LONG).show(); break; default: Toast.makeText(LocTestActivity.this, "出错鸟~~", Toast.LENGTH_LONG).show(); } } }; //设置相关参数 private void setLocationOption(){ LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开G option.setCoorType("bd09ll"); // 设置坐标类型 option.setServiceName("com.baidu.location.service_v2.9"); option.setPoiExtraInfo(true); option.setAddrType("all"); option.setScanSpan(15000); option.setPriority(LocationClientOption.GpsFirst); // 设置网络优先 option.setPoiNumber(10); option.disableCache(true); mLocClient.setLocOption(option); } @Override public void onDestroy() { mLocClient.stop(); super.onDestroy(); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.baiduloctest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:name="com.example.baiduloctest.Location" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.baiduloctest.LocTestActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> <!-- <intent-filter> <action android:name="com.baidu.location.service_v2.9"></action> </intent-filter>--> </service> </application> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission> <uses-permission android:name="android.permission.READ_LOGS"></uses-permission> <uses-permission android:name="android.permission.VIBRATE"></uses-permission> <uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission> <uses-permission android:name="android.permission.INTERNET" /> </manifest>