1、ajax获取数据示例
注意点:一定要在AndroidManifest.xml配置
<uses-permission android:name="android.permission.INTERNET" />
2、经纬度获取(离线状态下要确保周边有卫星获取)
3、语音tts插件
4、返回按钮失效
var isready=0;
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady(){
isready=1;
alert("初始化加载")
//点击与后台交互
document.getElementById('id1').addEventListener('click', clickHandler);
//位置实时变化--经纬度
document.getElementById('start_watch').addEventListener('click', clickStart);
document.getElementById('stop_watch').addEventListener('click', clickStop);
//文字转语音
document.getElementById('get_info').addEventListener('click', startupWin);
//后退失效
document.addEventListener('backbutton',onBackKeyDown,false);
}
/*****返回键被禁止*******/
function onBackKeyDown(e){
e.preventDefault();
}
/*******************************/
var textStr = '测试语音';
//文字转语音
function startupWin() {
TTS.speak({
text: textStr,
locale: 'en-GB',
rate: 1
})
}
/****************************/
//点击与后台交互
function clickHandler() {
if(isready==1){
var postUrl1 = "XXX";
$.ajax({
type: 'get',
dataType: 'json',
async: true,
url: postUrl1,
success:function(data){
console.log(data)
alert("访问成功",data);
},
error:function(jqXHR){
console.log(jqXHR)
alert("发生错误" ,jqXHR.status);
}
})
}
}
//初始化获取位置信息
function onSuccess(position){
alert("加载")
document.getElementById("Longitude").innerHTML="经度:"+position.coords.longitude;
document.getElementById("Latitude").innerHTML="纬度:"+position.coords.latitude;
}
function onError(){
alert("获取信息失败")
}
function clickStart(){
alert("点击开始")
navigator.geolocation.getCurrentPosition(onSuccess,onError,{ maximumAge: 0, timeout: 5000, enableHighAccuracy: true });
timer=setInterval("navigator.geolocation.getCurrentPosition(onSuccess,onError,{ maximumAge: 0, timeout: 5000, enableHighAccuracy: true })",5000);
}
function clickStop(){
if(isready==1){
alert("停止运行")
clearInterval(timer);
}
}