Sie sind auf Seite 1von 23

TPA - Windows Phone

Web requests, Location services Phone sensors

Prepared by: Kamil Kowalski

Agenda
Web Requests
WebClient HttpWebRequest Consuming Web Service

Location services Accelerometer Compass Gyroscope Q&A

Web Requests
WP7 uses the same Web request API as normal .NET
Only the asynchronous request methods available

System.Net offers the WebClient class for simple access


For non-trivial data handling the WebClient can cause problems while it runs in the UI thread, so HttpWebRequest should be preferred WebClient cannot be used when you need to access the basic HTTP protocol features

HttpWebRequest is for HTTP requests


Use BeginGetResponse() method to initialize a HTTP GET The response is available in the Callback method through the argument Typical approach is to get the response stream, and read the contents of the stream

Web Requests
WebClient
The WebClient does not support concurrent calls
You have to manage to wait for the completion of the previous call before starting another one

The WebClient class provides a number of ways to call an http resource:


DownloadStringAsync - provides the simplest way of downloading a string resource from the network, like an HTML page, a RSS or ATOM feed, a plain XML file and so on UploadStringAsync - it can be used to send strings to a server and it can also specify the method to be used to make the call. Probably it is the simplest way of making calls to a REST resource OpenWriteAsync & OpenReadAsync - these two method allow to send and receive binary resources to/from the server. The use is slightly more complex that the other methods but once the call completes you can access a binary stream you can read or write directly.

Web Requests
WebClient
How to use it the simplest way:

Web Requests
WebClient
How to use it beter way:

Web Requests
HttpWebRequest
Basic scenario:
Create an instance of the HttpWebRequest If you have to post something get the request stream Then write posted data to the request stream Get the response (that is actually what starts the call) Get the response stream Read returned data from response stream Close all thing

Web Requests
HttpWebRequest How to use it the simplest way:

Web Requests
HttpWebRequest How to use it beter way:

Web Requests
Consuming Web Service
The WebClient and HttpWebRequest are both complete and easy to use, the problem is that their usage is for sure heavy, because you need to rely on your own protocol made for example of XML files.
The process of serializing and deserializing can become very annoying. The real answer to this problem are the Web Services - something that is based on a known protocol (built on top of HTTP) and makes easy the process of sharing data between client and server, hiding transport and serialization problems.

Web Requests
Consuming Web Service
Working with Web Services you have to be aware of three things that uniquely define the Web Service you want to consume: Address, Binding and Contract.
Address - says where the service is located, in the form of a Uri Binding - describes how the client and server will speak each other. Serialization and deserialization matter is part of the binding but also the transport: HTTP, HTTPS, and so on. Contract - the most important part, that describes what the service can do.
In WCF, a contract is an interface with a bunch of methods - usually called Operations - and each one do something with some kind of input and output.

Web Requests
Consuming Web Service
Once you have the Address of the service you have to consume, the first thing you have to do is creating a Proxy. The proxy is a client side class that implements the Contract of the service and hides to the developer the presimageence of a Web Service.
What it happens is that you call a method on the local class and the proxy take care of serialize request, making the network call, reading the response and deserializes it.

Sample web services:


GeoIP http://www.webservicex.net/geoipservice.asmx?wsdl Stock Quote http://www.webservicex.net/stockquote.asmx?wsdl Demo Weather http://www.webservicex.net/globalweather.asmx?WSDL

Location services
Windows Phone uses three technologies for finding your location: GPS, WiFi based location, and Cell Tower based location (triangulation).

Location services
GPS technology relies on satellites in orbit and the phone's ability to receive signals from at least three of these satellites.
The satellites are transmitting all the time from synchronized clocks. But because of the satellites being different distances from the phone the time received from the satellites will be slightly different, a user's position can be calulated from these time differences.

Cell Tower based location and WiFi based location are similar to GPS in how they work.
When you use either one of these the phone will try to get the identifiers for near by WiFi access points and cell towers. The identifier is sent to a Microsoft service which keeps records of the general area in which those WiFi access points or cell towers have been seen and returns that information to the device.

Location services
Each one of these location technologies differs in the accuracy of the location that it provides.
GPS is the most accurate with the margin of error being around 10 meters in favourable conditions. The next most precise location technology is WiFi based location. WiFi access points don't transmit nearly as far as cell towers. Cell tower based location is the least precise.

Generally speaking the more precise location technology (GPS) also has a more significant impact on battery life.

Location services
Things to Remember
Request the Location Services capability in your WMAppManifest.xml

Ask the user for permission to get their location Be prepared for the possibility of location services being disabled on the phone Don't transmit the user's location to any service without their permission Only use high accuracy location when needed Turn off location services when it is nolonger needed Nokia Places API for Windows Phone

Accelerometer
The accelerometer is a sensor in the Windows Phone device that measures the acceleration on 3 axes (X, Y, Z), relative to freefall.

Accelerometer
In addition to a timestamp, the values are expressed in Gforces (1G = 9.81 m/s2).
It this means is that if the phone is lying face-up on a perfectly flat surface, the Z axis would read 1.0, and the other two axes would read 0.

How to get values from accelerometer basic scenario:


Instantiate a new Accelerometer object. Create a new ReadingChanged() event handler to monitor the changes in data. Pass the results of that event back to our pages thread (the event fires on a different thread). Use the data values in application.

Accelerometer
The emulator emulates accelerometer data by Accelerometer tab in Additional tools window.
Feature introduced in Mango release. By default the emulator assumes it is standing in portrait position, lined up on a flat surface.

Other ways to emulate accelerometer data:


use Reactive Extensions to emulate and filter accelerometer data for Windows Phone (check here) use accelKit (check here)

Compass
In traditional terms, a compass is used to determine the direction of the Earths magnetic north pole. Mobile phones are using element called magnetometer.
It can still determine the direction of magnetic north, but it can also determine rotation of the device relative to magnetic north. In addition, it is also capable of detecting magnetic fields around the device (which often interfere with those other calculations). The MagneticHeading is the heading, in degrees, measured clockwise from the Earths magnetic north. The TrueHeading is the heading, in degrees, measured clockwise from the Earths geographic north. The X, Y, and Z values that you retrieve from API are actually measured in microteslas, which are a unit of magnetic field strength.

NOTICE: The emulator doesnt emulate compas data!

Gyroscope
According to Wikipedia, a gyroscope is a device for measuring or maintaining orientation. It has the ability to spin on three axes, the X, Y, and Z axis
While the Accelerometer measures acceleration, the Gyroscope measures rotational velocity. The data you receive measures the rotational velocity of the device in radians per second. This means that you can more accurately and smoothly measure the current orientation of the device.

NOTICE: The emulator doesnt emulate gyroscope data!

Compas & Gyroscope


Optional hardware
Not all Windows Phones will have a Gyroscope and/or Compas. In fact, only phones that come out after the Mango release will be capable of having a Gyroscope and/or Compas It is still an optional piece of hardware. Microsoft has created a Motion class that combines the data from the Accelerometer, the Compass, and the Gyroscope into one class that we can use more effectively.

Q&A
??

Das könnte Ihnen auch gefallen