【小(xiǎo)編推薦】PhoneGap設備信息、網絡連接和(↓ '₽hé)通(tōng)知(zhī)

2014-05-21   &♦;|   發布者:梁國(guó)芳&∞γ←δnbsp;  |  &‌γnbsp;查看(kàn):3320次

app開(kāi)發
一(yī)、設備信息

device.name   &n↓<bsp;   設備名稱¥‌↓λ
device.cordova   &✘★&<nbsp; PhoneGap 版本

device.platform    設備平台γ↔
device.uuid    ≈​€ ;     uuid ♥‍編碼
device.version  &nb↕δsp;  平台版本

<script type="text/javascr®↔αipt">

   &nb✔βφ∑sp; $(document).ready(funcσα∞tion(){
      ¶δγ      ♣↔≈✘;  document.addEventLi  ​♣stener("deviceready","♥ myDeviceReadyListener, false);
     });

     functio←₹&n myDeviceReadyListe÷"♠ner(){
      ←♦    $("#devi★γceName .ui-btn-text")&≤Ω.text("設備名稱" + device.>↓name);

     &nb♦™αsp;    $("™₩δ#deviceCordova .ui-btn-text").tex>σt("PhoneGap 版本" + ₹♣↓♣device.cordova);
     &nb ₹sp;    $✔γδ("#devicePlatform .ui-btn-teβ ₹÷xt").text("∏"↑≠設備平台" + device.platform);
    <≥ ©;   &£₩nbsp;  $("#deviceUUID .&€ui-btn-text").text("uuid ≈♣≠₹編碼" + device.uuid);

   &nb∏π×♣sp;      $₹∏€("#deviceVersion .ui-btn-♦±¥↔text").text("平台版本&α≥$∑quot; + device.versi£§‍♦on);
   &nbs©‌p; }

</script>
</head>
<body>

     <div d‍∑ata-role="page">
      δ ¥    <divφ∞↕ data-role="header">δ₩
     &'σ≠↔nbsp;   &n•₽÷bsp;    <h1>Phon★♠↔eGap 實戰</h1>

    &nbs€ε↓ p;   &n♠← ∑bsp; </div>
    &nb¥×sp;     <div d↔$π✘ata-role="content">
    &n€λbsp;    &≠✘✘>nbsp;    &±↑lt;a href="#" data-role=&quo♣<∞t;button" id=&qu☆αot;deviceName"&≥§gt;</a>

   &n€→"↑bsp;    ✘€€    ¥↔;  <a href="#&q<★uot; data-role="button" id‌α‌​="deviceCordova"></a₩≠$>
      &n±Ω✘bsp;     ©π   <a href="#&quo↑↕£♣t; data-role="button" id‌✘="devicePlatform&qu€®δot;></a>

-------------------- ♦<--- Page 2-----------------------

     &₽∑↕≈nbsp;     ÷♠®→    <a href'←✘="#" data-role="button¶ ±​" id="deviceUUI€®D"></a>
     &nbΩπsp;   &n§σ​bsp;     <a  ÷href="#" data-role=&q$≤αuot;button" id="device ₽Version"></a>

    &nb✔δsp;   &™‍£nbsp; </div>
    ¶©;    &♠± nbsp; <div data-role=&q←βuot;footer">
     &♣λnbsp;    &n♠₩​φbsp;    <h4‌δ&‍>&nbsp;</h4&★σ‍gt;

    &  nbsp;     <♣'£λ;/div>
    ™∏; </div>
</body>

二、網絡連接

     Phonγ$→∞egap 網絡連接通(tōng)過naviga₽♥tor.network.connection.t↕★₽ype 來(lái)獲取,一(yī)般有γ∑€&(yǒu)一(yī)下(xià)幾種狀态
     1.&≠↕nbsp; Connection.UNKNOWN 未知(∏✔zhī)連接

     2.  C £≤φonnection.ETHERNET  以太網
     3. '≈€; Connection.WIFIWiFi
     4.  Con©±γ¶nection.CELL_2G2G 網絡

     5.  Co₹↔​€nnection.CELL_3G3G 網絡
     6.  Co™<←≥nnection.CELL_4G &nbε×∞sp;      π™;      &≈✔nbsp;    4G 網絡α←
     7.  Conne±≠Ωction.NONE &nbs∑©•p;    ↕≥;   &nλβ÷&bsp;    &nbsφ♦∏βp;      •₩; 無網絡連接

<!DOCTYPE html>

<html>
<head>
<title>Device Pr¶>₽operties Example</title&✘×βgt;

<meta http-equiv="Content-₹ ←type" content=&qφ↕♣©uot;text/html; charset=utf-8&qu★§§ot;>
<script type="text/$<♠£javascript" charset=&qγγ‌uot;utf-8" src=&δ±®quot;cordova.js"&​≠♠∑gt;</script>
<script type="÷£>text/javascript"§‍ " charset="utf-8">₽Ωδ';

     document.a¥±δ<ddEventListener("de×¥>✔viceready", onDeviceReady, fσ₩¶alse);

     func€$tion onDeviceReady()&φ← {
     &nb ®•÷sp;    ♣β↔±// 監聽(tīng)網絡的(de)變化(hu↓¥®à)
     &↔¥​ nbsp;    w↕÷indow.addEventListener("online&₽☆₽♣quot;, onOnline, false);

      ☆↕;    win®×dow.addEventListener("o"Ω♣↔ffline", onOffline, false);
     ​★∏;     ♦φ®// 檢查網絡連接
     &nb ‌γ&sp;    c✘ •heckNetworkConnection();

     }

     func★σ$tion checkNetworkConnectγπion() {

    &↔≤£↓nbsp;   &nb©∏sp; var states = {};
      §    ↓∑ states[Connection.UNKNOWN] &n‌✘✘bsp;    ±→   = '未知(zhī)連接';
     &n"✘λ☆bsp;    states≈σ[Connection.ETHERNET] = ' 以太網';

     &©§nbsp;    states[Connecti£↓↔®on.WIFI]  &nb¶<↔sp;     = 'WiFi';

----------------------- Page 3--------'¥ α---------------

     ¥↓₽€;      ∞φstates[Connection.CELL_2G] &nbs€ p;      =≠↓λ♠ '2G 網絡';
     ✘Ω;   &nb∑‌₽÷sp;  states[Connection.C↕↑±→ELL_3G]   &$<♣←nbsp;    = '3G 網絡';σ€

      Ω←;     states[δΩ♦Connection.CELL_4G] &nλ‌£bsp;   ™£¥$;   = '4G 網絡';
     &‌•nbsp;     stat★→✔es[Connection.NONE]  >ε     &nbσε>sp;   &↑<•≤nbsp; = '無網絡連接';
      &nb®÷£♦sp;    alert('網•Ω∑₽絡連接類型: ' + states[navigator.network.conφ ™σnection.type]);

      }

     funct↕±ion onOnline() {

     & φnbsp;     alert('您現™↓(xiàn)在在線');
     ≈₩ ∏ }

   &nb≥Ωsp; function onOffline() {
      ‍∏;     aler‍εt('您現(xiàn)在離(lí)線');
      }

</script>
</head>
<body>

<p>檢查網絡類型的(de)例子(zǐ)</p>$Ω ∑;
<input type="button" v≠✘γβalue="檢查網絡""check ±NetworkConnection()&qu∑✘ot; />
</body>

</html>

三.通(tōng)知(zhī)

    &nbsφ✘p;      notif↕π ±ication.alert 提示對(duì)話(huà)框

    π♣    &nbsΩ∑₩p;  notification.confirm 确認對(du→♥∞↕ì)話(huà)框
   &nbs​♥p;    &n←ε≈bsp;  notification.prompt 該♦δ ☆notification.prompt 方法顯示一(yī)₽&個(gè)對(duì)話(huà)框,比本地( ¶ dì)浏覽器(qì)的(de)提
示功能(néng)更強大(dà)。

     ↓×Ω    &n©∑bsp; notification.beep 發出嘟嘟的(de)聲××σ↑音(yīn)
     λ✔π ;      notifi×∑cation.vibrate 震動

<!DOCTYPE HTML>

<html>

<head>

<meta charset="utf-8">

<title>PhoneGaδ✔p-Notification</titl"γe>

<link rel="stylesheet"→ ≠; href="../jquery.mobile-1.1.•∞‌γ1/jquery.mobile-1.1.1.css&'✔$quot; />

<script src="../jq‌πuery.mobile-1.1.1/jquery.js"&↑β♣₽gt;</script>

<script src="../jquery.mobile-1★₩'.1.1/jquery.mobile-1.1.1.§∏js"></script>

<script type="text/javascrip←£♣&t" charset="u∏♣✘tf-8" src="../cordovε"±βa-1.9.0.js">♥≤≥‌</script>

<script type="te₹¥xt/javascript">

    βγφ≤; $(document).ready(funct±ελ×ion(){

----------------------- Pagσ₽εe 4-------------------↑∞®----

     &n‍±bsp;   &nbs✔→p;      ±♦¶;    document.addEven​λγ‍tListener("devicere≥₩ady", myDeviceRead×π$ yListener, false);

     &nΩ×™bsp; });

    £‌★•   function myDeviceR♠÷↔←eadyListener(){

      &✘‍✔nbsp;   δ©   $("#NoAlert&qu¥∞£Ωot;).click(function(){

      α↕    ♣₹;      &n★♠↑bsp;   navigatoλ ₹r.notification.alert("δ€♦這(zhè)是(shì)一(yī)個(gè)提醒&★™quot;,function(){

     ¥πγ";   &nb♣☆sp;     &n←≤‌≤bsp;   &nb™Ωsp;   &nb¶•sp;   console.log("确認了(l♠÷§e)!");

     &n✘βbsp;     &nb♦©sp;   &nb™<σsp;    },&≤Ω quot;标題","♥☆λ确認");

      &nbλ•σsp;    ↓ ;  });

    &♣✔→nbsp;   &•☆∞nbsp;    $("#N‍✔oConfirm").click(function∞α±(){

     ÷→γ≥;    ‍Ω×≠      ♦§     naviga→π♠πtor.notification.confirm("請(qǐng)問∑ε(wèn)你(nǐ)吃(chī)飯了(le↑€λ₽)嗎(ma)?",function(i"∑&nfo){

   &nbs♥αp;      ©     &nb✘₹☆∑sp;   γ↓β    ♥±    console.♠'log("你(nǐ)選擇了(l€ e)" + info);

   &nbsΩ≤p;     &nbs✘☆p;   &n₩™bsp;  &nbs₽ p;   },"請(qε₽φǐng)确認","吃(chī)了(le),沒吃(c§↔hī)");

     ♦"♥>     &nbs ααp;  });

      δπ';     &nb‌¥☆sp; $("#NoBeep").cliβ↓∞σck(function(){

    &nαΩ γbsp;     &nbs'¥¥₹p;     α≤;    navigator.noti‍↓>αfication.beep(3);

     &‍∏₽nbsp;   $≠★;    });

      &n&β∏δbsp;      $&₹ ★("#NoVibrate").click(funcσ¥♥Ωtion(){

    ↑¥§;    &nbs§≤λγp;   ≠α®☆      ≥   navigator.notification.α•vibrate(3000);

    &nbs↔←λp;    & β$nbsp;   });

    &nbs ↓←‌p;  }

</script>

</head>

<body>

    &nbsπ★p;  <div data-role="pag→δ¥₹e">

    >±≠☆;      &n∞↓δbsp;  <div data-rol≠↓π∏e="header">

     ±π      &nb₽Ωsp;     ±Ω÷₹   <h1>PhoneGap 實戰<​>/h1>

    £©      &β↕↕♣nbsp;  </div>

    <σ;      ≈£;   <div data-role="<→;content">

   &nα•₹×bsp;    &n←↑bsp;    ¶♦≥♦;    β★​;   <a href="#&quσ↑ot; data-role="button"σ★•; id="NoAlert&quo≈§t;>彈出提示框</a>™≥‌ 

    &nbs∏≈p;     ↓‌      &nb→α‌sp;   <a href="#​↓✔∞" data-role="button" β$±≈id="NoConfirm"<♠;>彈出确認框</a>

      &>≠≠>nbsp;   •←✘¥      ∞×✘    <a href="λ$;#" data-role="÷±α★;button" id="×γNoBeep">提示音(yīn)</a&g₹≈☆t;

      ♠'      &♠♠÷✔nbsp;     >‌≤Ω  <a href="→α;#" data-role="buttonλ™" id="NoViβ✔ ♦brate">震動</a>

   &nλ'∞bsp;   &n×∞"≥bsp;     </div&g™‍♣§t;

      ®✔;    ε×;   <div data-role=&quo'♥t;footer">

     &nb₩δ sp;   &nbs♣↑p;    &nb'↓>"sp;     <h4>&δσ∏↓amp;nbsp;</h4>

      §₽α    &n₽↔bsp;  </div>

     &φ≤nbsp; </div>

</body>

---------------------☆ §γ-- Page 5----------------∑>α≈-------

</html>