編輯:關於 Android 程式設計
應用一旦需要地圖與定位功能,直接把 SDK 打包進來往往會讓體積急劇膨脹,甚至可能觸及 65535 方法數的限制。此時以「插件」的方式接入是相當不錯的選擇。本文以 Apkplug 框架為例,記錄了將百度地圖與高德地圖分別作為插件接入宿主App的完整過程。
這裡示範把百度地圖做成插件並接入宿主:在宿主中點擊按鈕,彈出插件的定位介面。
最麻煩的其實是賬號與 key 的申請。key 的申請流程與高德類似,但使用時有明顯差異。進入百度地圖控制台:

點擊「創建應用」後:

其中 apk 簽名的 sha1 值取得方式請參考官方文件。
關鍵注意事項:一個應用要使用某個 key,簽名、包名、key 三者必須對應。由於地圖 SDK 是在插件中接入的,key 需要配置在插件裡;但百度地圖 SDK 讀取包名時讀的是宿主的包名,因此建立 key 時應直接使用宿主的包名,再把生成的 key 配置到插件。若想讓插件獨立測試,可另外為插件生成一套 key,但一旦併入宿主,就必須換回宿主對應的 key。
把 key 的問題理清後,剩下的就照百度地圖文件操作即可。在布局中加入地圖控件:
在 Activity 中初始化並啟動定位:
mMapView = (MapView) findViewById(R.id.bmapView);
baiduMap = mMapView.getMap();
baiduMap.setMyLocationEnabled(true); // 開啟定位圖層
mLocationClient = new LocationClient(this);
mLocationClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打開 GPS
option.setCoorType("bd09ll"); // 設定座標類型
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
mLocationClient.start();
先用插件獨立運行,能正常定位即算完成。接著為插件加上 plugin.xml:
宿主需要把百度地圖的權限配置過來,Android 6.0 以上記得手動申請權限,其餘按套路進行。啟動插件 Activity:
Intent intent = new Intent(); intent.setClassName(MainActivity.this, "com.apkplug.baidumapplug.MainActivity"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
若插件初始化較耗時,可改用異步啟動:
org.osgi.framework.Bundle[] bundles = FrameworkFactory.getInstance()
.getFrame().getSystemBundleContext().getBundles();
for (org.osgi.framework.Bundle bundle : bundles) {
if (bundle.getName().equals("BaiduMapPlug")) {
bundle.start(new StartCallback() {
@Override public void onSuccess(org.osgi.framework.Bundle bundle) {
Intent intent = new Intent();
intent.setClassName(MainActivity.this, "com.apkplug.baidumapplug.MainActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
@Override public void onFail(org.osgi.framework.Bundle bundle, Throwable throwable) {
Log.e(getClass().getName(), throwable.toString());
}
});
}
}
Demo 位址:
同樣把高德地圖的簡單定位功能放進插件,宿主點擊按鈕即跳轉到插件的定位介面。
完成高德註冊後,關鍵仍在於 key——高德的各項接口調用都會檢測 key。key 的管理在控制台:

在介面中加入地圖控件:
在程式碼中完成設定:
mlocationClient = new AMapLocationClient(getContext());
mLocationOption = new AMapLocationClientOption();
mMapView = getView().findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
aMap = mMapView.getMap();
aMap.setLocationSource(new LocationSource() {
@Override public void activate(final OnLocationChangedListener listener) {
mlocationClient.setLocationListener(aMapLocation -> listener.onLocationChanged(aMapLocation));
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
mlocationClient.setLocationOption(mLocationOption);
mlocationClient.startLocation();
}
@Override public void deactivate() {
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
mlocationClient = null;
}
});
aMap.getUiSettings().setMyLocationButtonEnabled(true);
aMap.setMyLocationEnabled(true);
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
單獨運行插件若能定位即完成;若回傳錯誤碼提示 key 有誤,請回頭檢查上面關於 key 的說明。接著加上 plugin.xml:
宿主按套路初始化、安裝插件,並依高德文件配置權限,最後用 Intent 啟動插件:
Intent intent = new Intent(); intent.setClassName(MainActivity.this, "com.apkplug.amapplug.MenuActivity"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
Demo 程式碼:
Android 基礎入門教程——10.1 TelephonyManager 電話管理器
Android 基礎入門教程——10.1 TelephonyManager(電話管理器) 本節引言 本章是 Android 基礎入門教程的最後一章,主要講解一些零散的知
Android 實作 TextView 兩端對齊的方法
Android 中的 TextView 預設是無法做到兩端對齊的,都是靠左對齊。可能的原因是 Android 預設不允許數字、字母作為第一行以後每行的開頭字符——因為數字
Android AlertDialog 四種常見用法詳解
需要展示進度、需要讓用戶確認一項操作、需要從幾個選項裡挑一個——這些需求都能用 AlertDialog 搞定。實用範圍內其實只有四種形態:普通提示框、單選框、多選框和進度
Android 多行水平滚动分页 GridView 实例源码详解
GridView 默认是垂直滚动的,但很多应用场景需要实现多行、水平滚动的分页效果,比如首页的运营位轮播。本文将带大家一起实现一个这样的自定义分页 GridView。