Unity gets power and network status for Android and iOS phone systems

Source: Internet
Author: User
<span id="Label3"></p><p><p>Initially consider using the intermediate static link library to invoke the mobile phone System's own api, But in the course of the study found that the Android system to record the power and other information in a fixed file, so only need to read in C # directly can not need intermediate library.</p></p><p><p>Version a.android</p></p><p><p>1. Direct read through C #, the Getbatterylevel () method below</p></p> <ol class="dp-c list-paddingleft-2"> <li><li><span class="keyword">int Getbatterylevel ()</span></li></li> <li class="alt"><li class="alt">{</li></li> <li><li><span class="keyword">Try</span></li></li> <li class="alt"><li class="alt">{</li></li> <li><li><span class="keyword">string capacitystring = System.IO.File.ReadAllText (<span class="string">"/sys/class/power_supply/battery/capacity"); </span></span></li></li> <li class="alt"><li class="alt"><span class="keyword">return <span class="keyword">Int.  Parse (capacitystring); </span></span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"><span class="keyword">Catch (Exception e)</span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt">Debug.Log (<span class="string">"Failed to read battery power;" + e.message); </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"><span class="keyword">return-1; </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p>Version B.ios</p></p><p><p>iOS needs to be written in Xcode. a static link library</p></p><p><p>1. Write the. mm file in xcode, implement C + + call iOS API to get the phone charge, part of the code is as Follows:</p></p><p><p>  </p></p>float getiosbatterylevel () {[[uidevice currentdevice] setbatterymonitoringenabled:yes];    return [[uidevice currentdevice] batterylevel]; }<p><p></p></p><p><p>2. Put the. a file Above. mm into the Unity Project Assets-plugins-ios (the Path cannot be wrong); in C #, use the following method Call:</p></p><pre class="prettyprint cs"><pre class="prettyprint cs"><span class="string">"__internal")]<span class="keyword"><span class="keyword"><span class="keyword"><strong>float getiosbatterylevel ()</strong>;<br><br></span></span></span></span>Call this function in C # to get iOS power</pre></pre><p><p>As for network conditions, Unity comes with an API to get</p></p><p><p>Application.interreachability</p></p>Networkreachability.notreachability Network unreachable networkreachability.reachableviacarrierdatanetwork via operator data network up to<br>Networkreachability.reachablevialocalareanetwork via local Area network (wifi)<p><p>Unity gets power and network status for Android and iOS phone systems</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.