自定义Application的使用 百度定位里面有个自定义application

这篇博客讲述了在Android开发中如何自定义Application,并结合百度定位服务进行使用。作者在自定义的MyApplication中初始化LocationClient和GeofenceClient,并实现BDLocationListener接口来接收位置信息。遇到的问题是由于在AndroidManifest.xml中未正确配置application标签导致程序报错,解决这个问题后,成功实现了高精度地理围栏回调功能。

 百度定位里面有个自定义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开始,哎

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值