|
plugin.xml:
<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova.baidu.yingyan" version="0.1.0">
<name>BaiduTrace</name>
<platform name="android">
<js-module src="www/Trace.js">
<clobbers target="window.navigator.baiduyingyan" />
</js-module>
<config-file target="res/xml/config.xml" parent="/*">
<feature name="BaiduTrace">
<param name="android-package" value="com.xuehexi.hello.BaiduTrace" />
</feature>
</config-file>
<source-file src="src/android/BaiduTrace.java" target-dir="src/com/xuehexi/hello" />
<source-file src="src/android/libs/arm64-v8a/libBaiduTraceSDK_v2_1_15.so" target-dir="libs/arm64-v8a" />
<source-file src="src/android/libs/armeabi/libBaiduTraceSDK_v2_1_15.so" target-dir="libs/armeabi" />
<source-file src="src/android/libs/armeabi-v7a/libBaiduTraceSDK_v2_1_15.so" target-dir="libs/armeabi-v7a" />
<source-file src="src/android/libs/x86/libBaiduTraceSDK_v2_1_15.so" target-dir="libs/x86" />
<source-file src="src/android/libs/x86_64/libBaiduTraceSDK_v2_1_15.so" target-dir="libs/x86_64" />
<source-file src="src/android/libs/BaiduTraceSDK_v2_1_15.jar" target-dir="libs" />
<config-file target="AndroidManifest.xml" parent="/*">
<!-- 这个权限用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!--允许程序连接到已配对的蓝牙设备-->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<!--允许程序发现和配对蓝牙设备-->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<!-- 用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SD卡读取权限,用户写入离线定位数据-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!--允许应用读取低级别的系统日志文件 -->
<uses-permission android:name="android.permission.READ_LOGS" />
<!--允许访问振动设备-->
<uses-permission android:name="android.permission.VIBRATE" />
<!--屏幕保持唤醒 不锁屏-->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!--允许应用程序更改主屏幕中的设置和快捷方式-->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<service
android:name="com.baidu.trace.LBSTraceService"
android:enabled="true"
android:exported="true" >
</service>
</config-file>
</platform>
</plugin>
Trace.js:
// baidu yingyan
var exec = require('cordova/exec');
var SERVICE_NAME = 'BaiduTrace';
var ACTION_START_TRACE = 'startTrace';
var ACTION_STOP_TRACE = 'stopTraceListener';
var ACTION_SET_INTERVAL = 'setInterval';
var ACTION_SET_LOCATION_MODE = 'setLocationMode';
var ACTION_SET_PROTOCOL_TYPE = 'setProtocolType';
var ACTION_QUERY_ENTITY_LIST = 'queryEntityList';
var ACTION_QUERY_HISTORY_TRACK = 'queryHistoryTrack';
var baiduyingyan = module.exports = {};
baiduyingyan.startTrace = function(success, error, entity) {
alert("调用");
exec(success, error, SERVICE_NAME, ACTION_START_TRACE, [entity]);
};
baiduyingyan.stopTraceListener = function(success, error, entity) {
exec(success, error, SERVICE_NAME, ACTION_STOP_TRACE, [entity]);
};
/**
* 鹰眼查询实时位置
*/
baiduyingyan.queryEntityList = function(success, error, entity){
exec(success, error, SERVICE_NAME, ACTION_QUERY_ENTITY_LIST, [entity]);
};
/**
* 查询历史轨迹
*/
baiduyingyan.queryHistoryTrack = function(success, error, entity){
exec(success, error, SERVICE_NAME, ACTION_QUERY_HISTORY_TRACK, [entity]);
};
BaiduTrace.java:
package com.xuehexi.hello;
public class BaiduTrace extends CordovaPlugin {
static String TAG = "BaiduTrace";
private LBSTraceClient client;
private Trace trace;
Context ctx;
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
System.out.println("初始化成功");
super.initialize(cordova, webView);
// your init code here
ctx = cordova.getActivity().getApplicationContext();
//实例化轨迹服务客户端
client = new LBSTraceClient(ctx);
}
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
System.out.println("开始");
JSONObject options = new JSONObject();
//开启鹰眼服务
if (action.equals("startTrace")) {
try {
options = args.getJSONObject(0);
}catch (JSONException e) {
callbackContext.error(e.getMessage());
}
System.out.println("开始");
//鹰眼服务ID
long serviceId = options.getLong("serviceId"); //开发者创建的鹰眼服务ID
//entity标识
String entityName = options.getString("entityName");
int traceType = 2;
//实例化轨迹服务
trace = new Trace(ctx, serviceId, entityName, traceType);
OnStartTraceListener startTraceListener = new OnStartTraceListener() {
@Override
public void onTraceCallback(int flag, String message) {
if(0 == flag || 10006 == flag || 10008 == flag){
callbackContext.success(message);
}else {
callbackContext.error(message);
}
}
@Override
public void onTracePushCallback(byte messageType, String message){
Log.v(TAG, "轨迹服务推送,并未使用");
}
};
client.startTrace(trace, startTraceListener);
//return getCurrentPosition(options, callbackContext);
return true;
}
if (action.equals("stopTraceListener")){
if (client==null || trace==null){
return false;
}
OnStopTraceListener stopTraceListener = new OnStopTraceListener(){
@Override
public void onStopTraceSuccess() {
if(callbackContext != null){
callbackContext.success();
}
}
@Override
public void onStopTraceFailed(int errorNo, String msg) {
if(callbackContext != null){
callbackContext.error(msg);
}
}
};
client.stopTrace(trace,stopTraceListener);
}
return false;
}
}
调用的点击事件:
Model.prototype.baiduTraceService1 = function(){
var entityName = "1234";
var serviceId = *****;
var entity = {
'entityName':entityName,
'serviceId':serviceId
};
var success = function(){
alert("成功")
};
var error = function(){
alert("失败")
};
var yingyan = window.navigator.baiduyingyan;
yingyan.startTrace(success, error, entity);
}
我在**.w文件中调用自定义的插件,可以取得对象,并且可以调用到Trace.js中的startTrace方法,手机上可以弹出Alter调用的内容,然后关闭弹出框后应用就会被强制关闭,出现错误,不知道有没有人知道是什么原因,源码已经贴上了。。。
|
|