AIDL学习笔记2之从Service获取地理位置
2011-12-19 13:38
381 查看
这个程序用到了百度地图的API,所以需要导入相应的包。下载地址:http://download.csdn.net/detail/qiaoning13256/3940259
之所以用百度地图API,是因为过google 的不怎么稳定,经测试,百度地图api还是比较稳定的。
怎么导网上有很多例子。
1、AIDL文件
ForMainActivity
2、Service
3、Activity
在onCreate()函数加上下边的代码:
4、Manifest文件
之所以用百度地图API,是因为过google 的不怎么稳定,经测试,百度地图api还是比较稳定的。
怎么导网上有很多例子。
1、AIDL文件
ForMainActivity
package com.android.aidl; interface ForMainActivity{ void performAction(double latitude, double longitude); }ForGetLocationService
package com.android.aidl; import com.android.aidl.ForMainActivity; interface ForGetLocationService{ void registCallback(ForMainActivity forMainActivity); }
2、Service
package com.android.service; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.LocationListener; import com.android.aidl.ForGetLocationService; import com.android.aidl.ForMainActivity; import com.android.util.Debugger; import android.app.Service; import android.content.Intent; import android.location.Location; import android.os.IBinder; import android.os.RemoteException; public class GetLocationService extends Service{ private ForMainActivity callBack; private LocationListener locationListener = null; private BMapManager mapManager; @Override public void onCreate() { Debugger.debug("GetLocation Service onCreate()."); init(); super.onCreate(); } @Override public void onDestroy() { Debugger.debug("GetLocation Service onDestroy()."); if (mapManager != null) { mapManager.destroy(); mapManager = null; } super.onDestroy(); } @Override public void onRebind(Intent intent) { Debugger.debug("GetLocation Service onRebind()."); super.onRebind(intent); } @Override public void onStart(Intent intent, int startId) { Debugger.debug("GetLocation Service onStart()."); if (mapManager != null) { mapManager.getLocationManager().requestLocationUpdates(locationListener); mapManager.start(); } super.onStart(intent, startId); } @Override public boolean onUnbind(Intent intent) { Debugger.debug("GetLocation Service onUnbind()."); return super.onUnbind(intent); } @Override public IBinder onBind(Intent intent) { Debugger.debug("GetLocation Service onBind()."); if (mapManager != null) { mapManager.getLocationManager().requestLocationUpdates(locationListener); mapManager.start(); } return mBinder; } private void init(){ // 初始化MapActivity mapManager = new BMapManager(getApplication()); // init方法的第一个参数需填入申请的API Key mapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4", null); mapManager.start(); locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { try { callBack.performAction(location.getLatitude(), location.getLongitude()); } catch (RemoteException e) { e.printStackTrace(); } } }; } private final ForGetLocationService.Stub mBinder = new ForGetLocationService.Stub() { @Override public void registCallback(ForMainActivity forMainActivity) throws RemoteException { Debugger.debug("callback 赋值"); callBack = forMainActivity; } }; }
3、Activity
private ForMainActivity.Stub callBack = new ForMainActivity.Stub() { @Override public void performAction(double latitude, double longitude) throws RemoteException { Debugger.debug("MainActivity callBack performAction excuted."); Debugger.debug("From service::latitude -> " + latitude + "," + "longitude -> " + longitude);
//这儿用Handler将得到的信息送出,这儿不能进行UI操作 Bundle bundle = new Bundle(); bundle.putDouble(LATITUDE, latitude); bundle.putDouble(LONGITUDE, longitude); Message message = new Message(); message.what = MSG_LOCATION_GOT; message.setData(bundle); handler.sendMessage(message); } }; ForGetLocationService forGetLocationService; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Debugger.debug("MainActivity onServiceDisconnected."); forGetLocationService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { Debugger.debug("MainActivity onServiceConnected."); forGetLocationService = ForGetLocationService.Stub.asInterface(service); try { forGetLocationService.registCallback(callBack); } catch (RemoteException e) { e.printStackTrace(); } } };
在onCreate()函数加上下边的代码:
Bundle bundle = new Bundle(); Intent intent = new Intent(MainActivity.this, GetLocationService.class); intent.putExtras(bundle); bindService(intent, connection, Context.BIND_AUTO_CREATE); startService(intent);
4、Manifest文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android" android:versionCode="1" android:versionName="@string/version_name" > <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <application android:icon="@drawable/qq" android:label="@string/app_name" > <activity android:configChanges="orientation|keyboardHidden|navigation" android:label="@string/app_name" android:name=".activities.MainActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Light" > </activity> <service android:name=".service.GetLocationService" android:process=":remote"></service> </application> </manifest>
相关文章推荐
- android菜鸟学习笔记31----Android使用百度地图API(二)获取地理位置及地图控制器的简单使用
- Silverlight学习笔记十七BingMap(七)之检索地理位置(GeocodeService服务)
- IOS学习笔记-获取当前的地理位置swift版
- 【转载】windows phone7 学习笔记14——地理位置服务与反应性扩展框架
- jQuery学习笔记(控件位置定位、尺寸大小的获取等)
- 【Android笔记 三】Location获取地理位置信息(中)Criteria类的简单使用
- 【js学习笔记-115】----html5之地理位置
- Android 学习笔记 Service服务与远程通信...(AIDL)
- windows phone7 学习笔记14——地理位置服务与反应性扩展框架
- [Silverlight学习笔记]关于利用WCF RIA Service进行通信并在客户端获取数据
- Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息
- IOS开发学习笔记(十七)——使用地理位置及地图(中篇)
- IOS开发学习笔记(十八)——使用地理位置及地图(下篇)
- ios学习笔记: IOS8解决获取位置坐标信息出错(Error Domain=kCLErrorDomain Code=0)
- 安卓学习笔记:获取我们当前所在的位置
- Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息
- IOS开发学习笔记(十六)——使用地理位置及地图(上篇)
- Service学习笔记-as下配置aidl
- html5 移动端获取触摸点位置学习笔记
- Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息