最近在做一个定位的应用,应用是在用户退出activity时仍能实现定位功能,这时想大家都会写在Service里,但是百度给的api和SDK都没有写在服务里的例子,刚开始我也是像写在activity里一样,可是老是报错,后来就换了百度的定位SDK,这个SDK也不能实现写在Service里,搞了一整天也没出来,网上也没有这方面的例子什么的,后来我想百度API出来这么久肯定有实现的方法吧,定位SDK刚出来没多久吧,所以还是转战了API
我劈开原来项目的复杂代码,写了个简单的Service测试,果然定位成功了,下面看源代码:
这个是百度给的demo其中一个类:没改动
public class BMapApiDemoApp extends Application {
static BMapApiDemoApp mDemoApp;
//百度MapAPI的管理类
BMapManager mBMapMan = null;
// 授权Key
// TODO: 请输入您的Key,
// 申请地址:http://dev.baidu.com/wiki/static/imap/key/
String mStrKey = "DF48619AD3E2DA5B3E0CAA8575DF9038035E1506";
boolean m_bKeyRight = true;// 授权Key正确,验证通过
// 常用事件监听,用来处理通常的网络错误,授权验证错误等
static class MyGeneralListener implements MKGeneralListener {
@Override
public void onGetNetworkState(int iError) {
Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(), "您的网络出错啦!",
Toast.LENGTH_LONG).show();
}
@Override
public void onGetPermissionState(int iError) {
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
// 授权Key错误:
Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(),
"请在BMapApiDemoApp.java文件输入正确的授权Key!",
Toast.LENGTH_LONG).show();
BMapApiDemoApp.mDemoApp.m_bKeyRight = false;
}
}
}
@Override
public void onCreate() {
mDemoApp = this;
mBMapMan = new BMapManager(this);
mBMapMan.init(this.mStrKey, new MyGeneralListener());
super.onCreate();
}
@Override
//建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗
public void onTerminate() {
// TODO Auto-generated method stub
if (mBMapMan != null) {
mBMapMan.destroy();
mBMapMan = null;
}
super.onTerminate();
}
}
这个是自己写的MainActivity,程序开始运行的类,在这个类里跳转到定位的Service里
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setClass(MainActivity.this,TestService.class);
startService(intent);
// this.finish();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
if (!app.m_bKeyRight) {
TextView text = (TextView)findViewById(R.id.location);
text.setText("请在BMapApiDemoApp.java文件输入正确的授权Key!\r\n" +
"申请地址:http://dev.baidu.com/wiki/static/imap/key/");
text.setTextColor(Color.RED);
}
super.onResume();
}
@Override
// 建议在APP整体退出之前调用MapApi的destroy()函数,不要在每个activity的OnDestroy中调用,
// 避免MapApi重复创建初始化,提高效率
protected void onDestroy() {
BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
if (app.mBMapMan != null) {
app.mBMapMan.destroy();
app.mBMapMan = null;
}
super.onDestroy();
}
这个是我写的测试类,实现定位功能的Service
public class TestService extends Service{
LocationListener mLocationListener = null;//create时注册此listener,Destroy时需要Remove
@Override
public void onCreate() {
BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
if (app.mBMapMan == null) {
app.mBMapMan = new BMapManager(getApplication());
app.mBMapMan.init(app.mStrKey, new BMapApiDemoApp.MyGeneralListener());
}
app.mBMapMan.start();
// 注册定位事件
mLocationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
if(location != null){
String strLog = String.format("您当前的位置:\r\n" +
"纬度:%f\r\n" +
"经度:%f",
location.getLongitude(), location.getLatitude());
Toast.makeText(TestService.this, strLog, Toast.LENGTH_LONG).show();
System.out.println("dsds:"+strLog);
}
}
};
super.onCreate();
}
@Override
public void onDestroy() {
BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
// 移除listener
app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
app.mBMapMan.stop();
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
// 注册Listener
app.mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);
app.mBMapMan.start();
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
在这个类里做了些转换,比如在activity里写在onPause代码里的我写在了onDestroy(),onResume()里的写在了onStart
程序第一次执行没问题,可以定位,可以Toast出来和打印出来,这时候Service是在后台运行的,这时如果你再发布的话,可能粗问题,就是空指针的问题,这个问题应该奥解决,我现在还没解决,不过大的问题解决了,就是定位可以写在Service里。明天再完善
用到的第三方工具是baidumapapi.jar和 libBMapApiEngine.so