百度定位里面有个自定义application 调了我个把小时,程序一直报错哎
终于找到问题的所在了
本应用 自定义了一个application
public class MyApplication extends Application {
public LocationClient mLocationClient;
public GeofenceClient mGeofenceClient;
private MyLocationListener mMyLocationListener;
public TextView mLocationResult;
public Vibrator mVibrator;
public NotifyLister mNotifyLister;
@Override
public void onCreate() {
super.onCreate();
mLocationClient = new LocationClient(this);
mMyLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);
mGeofenceClient = new GeofenceClient(this);
mNotifyLister = new NotifyLister();
mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
}
/**
* 实现实位回调监听
*/
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//Receive Location
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());
sb.append("\ndirection : ");
sb.append(location.getDirection());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\naddr : ");
sb.append(location.getAddrStr());
//运营商信息
sb.append("\noperationers : ");
sb.append(location.getOperators());
}
logMsg(sb.toString());
Log.i("BaiduLocationApiDem", sb.toString());
}
@Override
public void onReceivePoi(BDLocation arg0) {
}
}
/**
* 显示请求字符串
* @param str
*/
public void logMsg(String str) {
try {
if (mLocationResult != null)
mLocationResult.setText(str);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 高精度地理围栏回调
* @author jpren
*
*/
public class NotifyLister extends BDNotifyListener{
public void onNotify(BDLocation mlocation, float distance){
mVibrator.vibrate(1000);
}
}
}
运用自定义application最主要的
最重要的一点,在AndroidManifest.xml要设置application
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
搞了我好久,没接触过自定义applic开始,哎
这篇博客讲述了在Android开发中如何自定义Application,并结合百度定位服务进行使用。作者在自定义的MyApplication中初始化LocationClient和GeofenceClient,并实现BDLocationListener接口来接收位置信息。遇到的问题是由于在AndroidManifest.xml中未正确配置application标签导致程序报错,解决这个问题后,成功实现了高精度地理围栏回调功能。
1690

被折叠的 条评论
为什么被折叠?



