当前位置:操作系统 > 安卓/Android >>

AIDL学习笔记2之从Service获取地理位置

这个程序用到了百度地图的API,所以需要导入相应的包。下载地址: /2011/1219/20111219032516866.zip

之所以用百度地图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() { 补充:移动开发 , Android ,

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,