<span id="Label3"></p>A preface to the orientation of xamarin.android<p><p><span style="font-size: 18px;">By opening our application, we can find that more and more applications are using positioning to make our lives more convenient, so this chapter will learn how to develop positioning in xamarin.</span></p></p><p><p></p></p><p><p></p></p>Ii. preparatory work<p><p><span style="font-size: 18px;">Because our virtual machine is running locally on the computer, it is naturally impossible to locate it, but we can use the <em><strong>DDMS</strong></em> tool to help us Debug.</span></p></p><p><p></p></p><p><p><span style="font-size: 18px;">The first thing to determine is the directory where your Android SDK is located, and the reader can find it in the following ways:</span></p></p><p><p><em><span style="font-size: 18px;">Tools-"options</span></em></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-size: 18px;">Then the reader opens the <em><strong>Tools</strong></em> folder under the folder and we can see a lot of files with bat results, and when we open the file named <em><strong>Ddms.bat</strong></em> , we will see the following interface:</span></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-size: 18px;">From here we can manually send GPS location Information.</span></p></p><p><p></p></p><p><p>Note: The project also requires the following permissions</p></p><p><p></p></p><p><p></p></p>third, the text<p><p></p></p>1. Monitor changes in GPS location<p><p><span style="font-size: 18px;">In this section we will learn how to get the location manager and get a different location provider through the location manager, all of which will have multiple locations because our mobile phones are not only gps-based, but also base station and network positioning, of course, accuracy, power consumption are different, this gives us A more flexible way to control, let's open the <em><strong>MainActivity.cs</strong></em> file for the new project and write the following code in <em><strong>OnCreate</strong></em> :</span></p></p><pre><pre><span style="color: #008080;">1 <span style="color: #0000ff;"><span style="color: #0000ff;"><span style="color: #0000ff;">void<span style="color: #000000;"> OnCreate (bundle bundle)<span style="color: #008080;"><span style="color: #000000;"> {<span style="color: #008080;"><span style="color: #0000ff;">base<span style="color: #000000;">. OnCreate (bundle); <span style="color: #008080;"><span style="color: #000000;">Setcontentview (Resource.Layout.Main); <span style="color: #008080;">5 Locationmanager lm =<span style="color: #000000;"> (locationmanager) getsystemservice (locationservice); <span style="color: #008080;"> 6}</span> </span> </span> </span></span> </span> </span></span> </span></span> </span> </span></span></span></span></pre></pre><p><p></p></p><p><p> <span style="font-size:18px;"> There are a lot of services in Android that are acquired in this way, so the reader must have this habit, rather than just calling a class just like local development, where we need to <em> <strong> Getsystemservice </strong> </em> Gets the specified name of the service, such as our location manager, with the location manager, we can get the location provider, monitoring location changes and so On. In order to be able to see the changes very clearly, we will listen to GPS location information changes, and through Toast display, first we let <em> <strong> mainactivity </strong> </em> Implementation <em> <strong> Ilocationlistener the method in the </strong> </em> interface, and writes the following code in <em> <strong> onlocationchanged </strong> </em>: </span> </p></p><pre><pre><span style="color: #008080;">1 <span style="color: #0000ff;"><span style="color: #0000ff;">void<span style="color: #000000;"> onlocationchanged (location location)<span style="color: #008080;"><span style="color: #000000;"> {<span style="color: #008080;">3 String s = String.Format (<span style="color: #800000;">"<span style="color: #800000;">{0} {1}<span style="color: #800000;">" <span style="color: #000000;">, Location. longitude, Location. Latitude); <span style="color: #008080;"><span style="color: #000000;">toast.maketext (applicationcontext, s, toastlength.short). Show (); <span style="color: #008080;">5}</span> </span></span> </span> </span> </span> </span> </span> </span></span> </span> </span></span></span></pre></pre><p><p></p></p><p><p><span style="font-size: 18px;">finally, we need to register it through the location manager, just write the following code at the end of the oncreate:</span></p></p><pre><pre><span style="color: #008080;"><span style="color: #800080;"><span style="color: #0000ff;">this);</span></span></span></pre></pre><p><p><span style="font-size: 18px;">The approximate meaning of this method is to track the change of GPs position, and every 5 seconds to refresh, and two times the location of the interval to 500 meters, according to the actual test of the author does not call your method every 5 seconds, but need to meet at the same time, so later readers will find that even if the GPS location changes will not show changes, This is due to the fact that the position of the change is too small.</span></p></p><p><p></p></p><p><p><span style="font-size: 18px;">After we change the GPS position via ddms, we can see the following prompt:</span></p></p><p><p></p></p><p><p></p></p><p><p></p></p>2. Get the location provider<p><p><span style="font-size: 18px;">We already know that there are multiple location providers, but the reality is not all of the providers we can use, some may be closed, some may be open, then we need to know which location providers are available, only through the location provider can be done, the specific code is as Follows:</span></p></p><pre><span style="color: #008080;">1 [Activity (Label =<span style="color: #800000;">"<span style="color: #800000;">Locationstudy<span style="color: #800000;">", Mainlauncher =<span style="color: #0000ff;">true, Icon =<span style="color: #800000;">"<span style="color: #800000;">@drawable/icon<span style="color: #800000;">"<span style="color: #000000;">)]<span style="color: #008080;">2<span style="color: #0000ff;">Public<span style="color: #0000ff;">Class<span style="color: #000000;">Mainactivity:listactivity<span style="color: #008080;">3<span style="color: #000000;">{<span style="color: #008080;">4<span style="color: #0000ff;">Protected<span style="color: #0000ff;">Override<span style="color: #0000ff;">void<span style="color: #000000;"><span style="color: #000000;" "> OnCreate (bundle bundle) <span style=" color: #008080; "> 5 <span style="color: #000000;" "> {<span style=" color: #008080; "> 6 <span style=" color: #0000ff; ">base<span style=" Color: #000000; ">. OnCreate (bundle); <span style=" color: #000000; " "7 locationmanager lm="<span"> (locationmanager) Getsystemservice (locationservice); <span style="color: #008080;"> 8 ilist<string> list = Lm. Getproviders (<span style="color: #0000ff;">true<span style="color: #008080;" ]; <span> 9 ListAdapter = <span style="color: #0000ff;">new arrayadapter<<span style="color: #0000ff;">string> (<span style="color: #0000ff;">this<span style="color: #008080;" , android.resource.layout.simplelistitem1, list); <span>10 <span style="color: #000000;">}<span style="color: #008080;">11} </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><p><p></p></p><p><p></p></p><p><p><span style="font-size: 18px;">After the successful operation, we can see what the current open location provider is, The following is the author Virtual:</span></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-size: 18px;">We can then select a location provider from which to get the <em><strong>Getprovider</strong></em> method of the location provider, in addition to using the above methods can also get the specified location provider, such as the code will get three different location providers:</span></p></p><pre><pre><span style="color: #008080;">1 locationprovider Lpgps =<span style="color: #000000;"> lm. Getprovider (locationmanager.gpsprovider); <span style="color: #008080;">2 Locationprovider lpnet =<span style="color: #000000;"> lm. Getprovider (locationmanager.networkprovider); <span style="color: #008080;">3 Locationprovider LPPSV = lm. Getprovider (locationmanager.passiveprovider);</span> </span> </span> </span></span></pre></pre><p><p></p></p><p><p></p></p><p><p><span style="font-size: 18px;">The above methods in practice will be more troublesome, so we also introduce another way to get the location provider, it is based on conditions, through our conditions, the Location Manager will choose an optimal location manager to us, such as the following code, we will get a location provider that meets this condition:</span></p></p><pre><span style="color: #008080;">1 Locationmanager lm =<span style="color: #000000;">(locationmanager) Getsystemservice (locationservice);<span style="color: #008080;">2 Criteria CRI =<span style="color: #0000ff;">New<span style="color: #000000;">Criteria ();<span style="color: #008080;">3<span style="color: #008000;">//<span style="color: #008000;">Accuracy<span style="color: #008080;">4 Cri. accuracy =<span style="color: #000000;">accuracy.coarse;<span style="color: #008080;">5<span style="color: #008000;">//<span style="color: #008000;">Energy dissipation<span style="color: #008080;">6 Cri. Powerrequirement =<span style="color: #000000;">power.low;<span style="color: #008080;">7<span style="color: #008000;">//<span style="color: #008000;">Altitude accuracy<span style="color: #008080;">8 Cri. altituderequired =<span style="color: #0000ff;">False<span style="color: #000000;">;<span style="color: #008080;">9<span style="color: #008000;">//<span style="color: #008000;">Direction accuracy<span style="color: #008080;">The Cri. Bearingaccuracy =<span style="color: #000000;">accuracy.low;<span style="color: #008080;">11<span style="color: #008000;">//<span style="color: #008000;">Whether to spend<span style="color: #008080;">Cri. costallowed =<span style="color: #0000ff;">False<span style="color: #000000;">;<span style="color: #008080;">13<span style="color: #008000;">//<span style="color: #008000;">Horizontal direction accuracy<span style="color: #008080;">Cri. Horizontalaccuracy =<span style="color: #000000;">accuracy.low;<span style="color: #008080;">15<span style="color: #008000;">//<span style="color: #008000;">Speed accuracy<span style="color: #008080;">Cri. Speedaccuracy =<span style="color: #000000;"><span style="color: #000000;"> accuracy.low; <span style="color: #008080;">17 <span style="color: #008000;">//<span style="color: #008000;"> Do you have speed capability <span style="color: #008080;">18 Cri. speedrequired = <span style="color: #0000ff;">false<span style="color: #000000;">;<span style="color: #008000;" <span>//<span style="color: #008000;"> vertical precision <span style="color: #008080;">20 Cri. Verticalaccuracy =<span style="color: #000000;" "> accuracy.low;<span style=" color: #008080; "> <span style="color: #008080;">22 <span style="color: #008000;">//<span style="color: #008000;"> Get the best location provider by condition Span style= "color: #008080;" >23 String pidstr = lm. Getbestprovider (cri, <span style="color: #0000ff;">true<span style="color: #008080;" ); <span locationprovider lp="Lm." getprovider (pidstr); < span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><p><p><span style="font-size: 18px;">With the comments in the code, we are able to take control of the required features and get the corresponding location Provider. Location information is not necessary to listen to determine the current location in real time, we can also use the location Manager's <em><strong>requestsingleupdate</strong></em> method to achieve only once, of course, this method is still a bit cumbersome, but also need location information update, There is a way to get information directly to the last location update, so you can avoid the location information must be updated again, it is like before your app opens, the previous app has updated location information, then open your app and then do not need to retrieve again, this Method's call is as Follows:</span></p></p><pre><pre><span style="color: #008080;">1 locationmanager lm =<span style="color: #000000;"> (locationmanager) getsystemservice (locationservice); <span style="color: #008080;"> 2 location LC = LM. Getlastknownlocation (locationmanager.gpsprovider);</span> </span></span></pre></pre><p><p></p></p><p><p><span style="font-size: 18px;">The method obtains the location information of the last gps, but there is no location update before this, then the returned location information may be wrong, or does not exist.</span></p></p><p><p></p></p><p><p></p></p>3. Track location changes<p><p><span style="font-size: 18px;">In the first example we took advantage of an interface to receive location updates, but the best way to do this in Android is not, and we can also use <em><strong>broadcast receivers</strong></em> to receive these location updates, and we'll Learn how to use that approach to receive location Updates. First we need to create a new broadcast receiver:</span></p></p><pre><span style="color: #008080;">1<span style="color: #000000;">[broadcastreceiver ()]<span style="color: #008080;">2<span style="color: #0000ff;">Public<span style="color: #0000ff;">Class<span style="color: #000000;"><span style="color: #000000;" "> locationbroadcast:broadcastreceiver<span style=" color: #008080; ">3 <span style=" Color: #000000; "> {<span style="color: #008080;">4 <span style="color: #0000ff;">public <span style="color: #0000ff;">override Span style= "color: #0000ff;" >void<span style="color: #000000;"> onreceive (context context, Intent Intent) <span style="color: #008080;">5 <span style="color: #000000;"> {<span style="color: #008080;">6 location LC =<span style="color: #000000; Extras.get (locationmanager.keylocationchanged); <span style= " color: #008080;">7 toast.maketext (context, lc. Longitude + <span style="color: #800000;"> "<span style=" color: #800000; ">" +<span style="color: #000000;"> lc. Latitude, toastlength.short). Show (); <span style="color: #008080;">8 <span style="color: #000000;">}<span style="color: #008080;">9} </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><p><p></p></p><p><p><span style="font-size: 18px;">The content is the same as in the first section, with the toast showing the updated Location.</span></p></p><p><p></p></p><p><p><span style="font-size: 18px;">Next we need to use another overloaded method of <em><strong>Requestlocationupdates</strong></em> to implement registering the broadcast receiver, as shown in the Code below:</span></p></p><pre><pre> <span style="color: #008080;">1 locationmanager lm =<span style="color: #000000;"> (locationmanager) Getsy Stemservice (locationservice); <span style="color: #008080;">2 <span style="color: #0000ff;">var tent = <span style="color: #0000ff;">new Int ENT (<span style="color: #0000ff;">this, <span style="color: #0000ff;">typeof<span style="color: #000000;"> ( locationbroadcast)); <span style="color: #008080;">3 <span style="color: #0000ff;">var ptent = pendingintent.getbroadcast (<span style=" Color: #0000ff; ">this, <span style="color: #800080;">0<span style="color: #008080;" ", tent, pendingintentflags.updatecurrent); span>4 Lm. Requestlocationupdates (locationmanager.gpsprovider, <span style="color: #800080;">5000, <span style="color: #800080;">100, ptent); </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></pre></pre><p><p><span style="font-size: 18px;">This one of the author is not much to explain, as long as it is in accordance with this tutorial learning should understand what meaning.</span></p></p><p><p></p></p><p><p></p></p>4. Imminent Warning<p><p><span style="font-size: 18px;">This concept is very easy to understand, naturally with GPS positioning, then we can set up a region, when the user enters or leave the area can notify our app, below we will be slightly modified above to achieve near warning, the first is the broadcast receiver:</span></p></p><pre><span style="color: #008080;">1<span style="color: #000000;">[broadcastreceiver ()]<span style="color: #008080;">2<span style="color: #0000ff;">Public<span style="color: #0000ff;">Class<span style="color: #000000;">Locationbroadcast:broadcastreceiver<span style="color: #008080;">3<span style="color: #000000;">{<span style="color: #008080;">4<span style="color: #0000ff;">Public<span style="color: #0000ff;">Override<span style="color: #0000ff;">void<span style="color: #000000;">OnReceive (context context, Intent Intent)<span style="color: #008080;">5<span style="color: #000000;">{<span style="color: #008080;">6<span style="color: #0000ff;">If (intent. Getbooleanextra (locationmanager.keyproximityentering,<span style="color: #0000ff;">True<span style="color: #000000;">))<span style="color: #008080;">7<span style="color: #000000;"><span style="color: #000000;" "> {<span style=" color: #008080; "> 8 Toast.maketext (context, <span style="color: #800000;"> "<span style=" color: #800000; ">entering<span style=" color: #800000; ">" <span style="color: #000000;">, toastlength.short). Show (); <span style=" color: #000000; " "9 <span>}<span style=" color: #008080; ">10 <span style=" Color: #0000ff; ">else<span style="color: #008080;">11 <span style="color: #000000;"> {<span style="color: #008080;">12 Toast.maketext (context, <span style="color: #800000;"> "<span style=" color: #800000; ">exiting<span style=" color: # 800000; "> "<span style="color: #008080;" ", toastlength.short). show (); <span>13 <span style="color: #000000;">}<span style="color: #008080;">14 <span style=" Color: #000000; ">}<span style="color: #008080;">15} </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><p><p></p></p><p><p><span style="font-size: 18px;">Here we can get the current to leave this area or enter this area through <em><strong>keyproximityentering</strong></em> , the corresponding registration part is also very simple:</span></p></p><pre><span style="color: #008080;">1<span style="color: #0000ff;">Protected<span style="color: #0000ff;">Override<span style="color: #0000ff;">void<span style="color: #000000;">OnCreate (bundle Bundle)<span style="color: #008080;">2<span style="color: #000000;">{<span style="color: #008080;">3<span style="color: #0000ff;">Base<span style="color: #000000;">. OnCreate (bundle);<span style="color: #008080;"><span style="color: #008080;">4 locationmanager lm =<span style="color: #000000;"> (locationmanager) Getsystemservice (locationservice); <span style="color: #008080;">5 <span style="color: #0000ff;">var tent = <span style="color: #0000ff;">new Intent ( Span style= "color: #0000ff;" >this, <span style="color: #0000ff;">typeof<span style="color : #008080; " (locationbroadcast)); <span>6 <span style="color: #0000ff;">var ptent = pendingintent.getbroadcast (<span style="color: #0000ff;">this,-<span style="color: #800080;">1, tent, <span style="color: #800080;">0<span style="color: #800080;" ); <span 7 lm. addproximityalert (<span>38.422006,-<span style="color: #800080;">110.084095, 5000f,-< Span style= "color: #800080;" >1<span style="color: #008080;" , ptent); <span>8} </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></pre><p><p><span style="font-size: 18px;">The first three parameters of the <em><strong>Addproximityalert</strong></em> are specified in this range, the number one and the second are fixed a point, the third is the radius, the fourth parameter is the timeout time, here the author set to 1 means no timeout, the last of course is our broadcast Receiver.</span></p></p><p><p>Xamarin.android Development Practice (17)</p></p></span>
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