2012-10-30 80 views
0

我如何獲取下面的這些項目以從設備讀取數據並在我的html頁面中顯示?Android手機應用程序讀取設備信息

$('#devicename').html(device.name); 
    $('#devicephonegap').html(device.phonegap); 
    $('#devicplatform').html(device.platform); 
    $('#deviceuuid').html(device.uuid); 
    $('#deviceversion').html(device.version); 

整版代碼

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<meta name="format-detection" content="telephone=no" /> 
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 
<link rel="stylesheet" type="text/css" href="jquery.mobile-1.2.0.css" /> 
<script type="text/javascript" src="jquery-1.8.2.js"></script> 
<script type="text/javascript" src="jquery.mobile-1.2.0.js"></script> 
<script type="text/javascript"> 

    $(document).on("pageinit", "#newpage", function() { 
     $('#saveButton').click(function() { 
      localStorage.setItem("name", $('#name').val()); 
     }); 


    }); 
    var wID = navigator.accelerometer.watchAcceleration(onSucess, onerror, { frequency: 1000 }); 
    function onSucess(a) { 
     $('#aX').html(a.x); 
     $('#aY').html(a.y); 
     $('#aZ').html(a.z); 
     $('#aTime').html(a.timestamp); 
    } 
    function onError() { 

    } 
var phoneName = window.device.name; 
var phoneName = device.name; 
     $('#devicename').html(device.name); 
     $('#devicephonegap').html(device.phonegap); 
     $('#devicplatform').html(device.platform); 
     $('#deviceuuid').html(device.uuid); 
     $('#deviceversion').html(device.version); 


    $(document).on('pageshow', '#newpage', function() { 
     var personName = localStorage.getItem("name"); 
     if (personName.length > 0) { 
      $('#name').val(personName); 
     } 
    }); 
</script> 
<title>Hello World 2</title> 
</head> 
<body> 
<div id="home" data-role="page"> 
    <div data-role="header"> 
     <h1>Home Page2</h1> 
    </div> 
    <div data-role="content"> 
     hello Phone Gap and JQuery Mobile! 
     <a href="#newpage" data-role="button">new page</a> 
     <br> 
     <p id="devicename"> </p> 
     <p id="devicephonegap"> </p> 
     <p id="deviceplatform"> </p> 
     <p id="deviceuuid"> </p> 
     <p id="deviceversion"> </p> 
     <p id="ax"> </p> 
     <p id="ay"> </p> 
     <p id="az"> </p> 
     <p id="aTime"> </p> 
    </div> 
    <div data-role="footer" data-position="fixed"> 
    <a href="#dialogpage" data-rel="dialog" data-icon="plus">Add Something</a> 

    </div> 
</div> 


<div id="newpage" data-role="page"> 
    <div data-role="header"> 
    <a href="#home" data-icon="delete">Cancel</a> 
     <h1>New Page</h1> 
     <a href=#home" data-icon="save">save</a> 
    </div> 
    <div data-role="content"> 
     <label for="name">what is your name?</label> 
     <input id="name" type="text" name="name" value="" /> 
     <a id="saveButton" href="" data-role="button">Save</a> 
    </div> 
    <div data-role="footer" data-position="fixed"> 
     <h4> 
      footer</h4> 
    </div> 
</div> 

<div id="dialogpage" data-role="page"> 
<div data-role="header"> 
     <h1>Dialog</h1> 
    </div> 
    <div data-role="content"> 
     this is a dialog 
    </div> 
</div> 
<script type="text/javascript" src="cordova-2.1.0.js"></script> 
<script type="text/javascript" src="js/index.js"></script> 
<script type="text/javascript"> 
    app.initialize(); 
</script> 
</body> 
</html> 

回答

1

您需要包括設備插件,並要求爲Android READ_PHONE_STATE權限。這是在明確記載:

http://docs.phonegap.com/en/2.1.0/cordova_device_device.md.html#Device_permissions

+0

感謝您的鏈接。我沒有app/res/xml/plugins.xml其稱爲配置。它確實有這一行代碼。在我的清單中,我確實擁有讀取手機狀態許可權。 – CsharpBeginner

+0

這些線會超出我上面的代碼嗎? //這些引用相同的'device' var phoneName = window.device.name; var phoneName = device.name; – CsharpBeginner

+0

好吧,現在我看到你的代碼,我可以看到問題。您需要先等待deviceready事件,然後才能訪問任何PhoneGap API或值。 –

1

-你需要READ_PHONE_STATE許可添加到您的AndroidManifest.xml文件。

+0

是的,我有。所以我想它不是我擁有的權限問題。 – CsharpBeginner