首页 > 安全资讯 >

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

11-12-19

这个程序用到了百度地图的API,所以需要导入相应的包。下载地址:http://download.csdn.net/detail/qiaoning13256/3940259之所以用百度地图API,是因为过google 的不怎么稳定,经测试,百度地图api还是比较稳定...

这个程序用到了百度地图的API,所以需要导入相应的包。下载地址: http://up.2cto.com/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() { 
         
        @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> 

摘自 qiaoning13256的专栏

相关文章
最新文章
热点推荐