AIDL学习笔记2之从Service获取地理位置
来源:互联网 发布:如何投诉淘宝低价销售 编辑:程序博客网 时间:2024/05/17 06:01
这个程序用到了百度地图的API,所以需要导入相应的包。下载地址:http://download.csdn.net/detail/qiaoning13256/3940259
之所以用百度地图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;@Overridepublic void onCreate() {Debugger.debug("GetLocation Service onCreate().");init();super.onCreate();}@Overridepublic void onDestroy() {Debugger.debug("GetLocation Service onDestroy().");if (mapManager != null) {mapManager.destroy();mapManager = null;}super.onDestroy();}@Overridepublic void onRebind(Intent intent) {Debugger.debug("GetLocation Service onRebind().");super.onRebind(intent);}@Overridepublic void onStart(Intent intent, int startId) {Debugger.debug("GetLocation Service onStart().");if (mapManager != null) {mapManager.getLocationManager().requestLocationUpdates(locationListener);mapManager.start();}super.onStart(intent, startId);}@Overridepublic boolean onUnbind(Intent intent) {Debugger.debug("GetLocation Service onUnbind().");return super.onUnbind(intent);}@Overridepublic IBinder onBind(Intent intent) {Debugger.debug("GetLocation Service onBind().");if (mapManager != null) {mapManager.getLocationManager().requestLocationUpdates(locationListener);mapManager.start();}return mBinder;}private void init(){// 初始化MapActivitymapManager = new BMapManager(getApplication());// init方法的第一个参数需填入申请的API KeymapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4", null);mapManager.start();locationListener = new LocationListener() {@Overridepublic void onLocationChanged(Location location) {try {callBack.performAction(location.getLatitude(), location.getLongitude());} catch (RemoteException e) {e.printStackTrace();}}};}private final ForGetLocationService.Stub mBinder = new ForGetLocationService.Stub() {@Overridepublic void registCallback(ForMainActivity forMainActivity)throws RemoteException {Debugger.debug("callback 赋值");callBack = forMainActivity;}};}
3、Activity
private ForMainActivity.Stub callBack = new ForMainActivity.Stub() {@Overridepublic 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() {@Overridepublic void onServiceDisconnected(ComponentName name) {Debugger.debug("MainActivity onServiceDisconnected.");forGetLocationService = null;}@Overridepublic 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>
- AIDL学习笔记2之从Service获取地理位置
- Android学习--Service之AIDL
- Android Service学习之AIDL
- Service学习笔记-as下配置aidl
- Android Service学习之AIDL实例
- Android Service学习之AIDL实例
- android服务学习service之AIDL
- 【js学习笔记-115】----html5之地理位置
- android学习笔记之AIDL
- android学习笔记之AIDL
- Android学习笔记之AIDL
- Service个人笔记2(aidl)
- 从 Remote Service Binding 学习 AIDL 与 IPC
- 从 Remote Service Binding 学习 AIDL 与 IPC
- Service以及AIDL学习
- Android Service 之 AIDL
- Service 之 AIDL
- Service之AIDL
- wince 播放器 最终版本
- linux内核sk_buff的结构分析
- mysql 联合索引
- 暂无
- WTL的安装
- AIDL学习笔记2之从Service获取地理位置
- sync、fsync和fdatasync函数区别
- 泰妍 만약에(如果)
- 上海居住证办理 续办上海居住证全攻略
- 工作学习笔记——GDI泄露检测利器
- 通过调整Linux TCP参数 提示服务器性能
- 51实现gsm的发短信收短信的程序
- 第一次重组代码
- 在JBoss中开发EJB容器模型的WebService