Thứ Ba, 20 tháng 11, 2012
Effortless English - change
Effortless English - change
NO AFFECTION ANYMORE
DEAR ABBY:
My husband of 39 years no longer hugsme or shows me any affection. I have seen him through
cancer and diabetes. We have four grown children, and we have been seeinga therapist for a year
and a half. He is kind, generous and friendly, but there are no complimentsor any of the flirtatious
banterwe used to enjoy.
He swears he's not having an affair, and he doesn't know why he has changed. Perhaps you do?
-- GRACE IN PHOENIX
Changed
Thứ Năm, 15 tháng 11, 2012
1. Introduction
To integrate our SDK as a publisher for Metaps, there are multiple ways to both display and
receive confirmation for Offers on the ad network. Before implementation, please consider
which method you will use to integrate, and choose the method that best matches your app.
How to use the Metaps Offerwall
The method available to display Offers depends on the type of app, as illustrated in the
diagram below.
How to receive Reward Confirmation
There are two ways to be notified of confirmation that the end-user can receive their reward
and publisher is entitled to a reward payout (Reward Confirmation). You can choose to
receive the confirmation from within the app, or have a notification sent to the server of your
choosing.
The meanings of the values above are as follows:
net.metaps.sdk.CLIENT_ID: Metaps ID
An unique identification assigned to advertisers and media. This can be confirmed at the
Login screen.
net.metaps.sdk.APP_ID: App ID
An unique identification assigned by metaps to each registered app. This can be
confirmed at the Login screen.
net.metaps.sdk.APP_KEY: Authentication key
An unique identification assigned by metaps for authentication purposes. This can
be confirmed at the Login screen.
net.metaps.sdk.PROMOTION_ID: Promotion ID
A string (up to 16 characters) that can be used for identifying your promotion
campaign.
net.metaps.sdk.MARKET: Market
A string (up to 16 character) that can be used to identify your intended market.
net.metaps.sdk.MODE: Test/Release Indication
1=Test/Debug, 0=Release
Add Resource Files to Project
Inside the downloaded SDK zip file, there is a folder named "res". This folder contains the
layout files necessary for your project to display the Offerwall correctly. Copy all these files
into your project, taking care not to overwrite any existing resources that you may have
already created, such as strings.xml.
If you already have an existing strings.xml, you should append the string resources
required by the SDK to your existing strings.xml.
Each file in the “res” folder is explained below. You can freely customize the design of the
layouts by editing or replacing files, as long as the names of the files/layouts remain the
same.
To ensure these layouts work properly, a specified ID must be used. Details regarding
these IDs are explained later in this documentation. If any of the IDs are missing, your app
may not work properly, so please pay close attention when customizing.
Important: Be careful not to overwrite your existing strings.xml, or forget to
append the Metaps SDK strings to your existing file.
It is possible to overwrite the “app_name” value as set in strings.xml to whatever
you want or use.
If you require other languages than English / Japanese / Chinese, it is possible to
create respective resource folders to support that language. If creating these folders,
please adhere to the Android standard naming conventions.
Settings for the Publisher
It is necessary to fill in the following publisher app settings from the Metaps dashboard.
After you login to the dashboard, select “Developer” “Monetize” then click on the “Edit”
button under “Reward Edit” for the registered app.
The fields that can be edited are listed below.
Title This string is shown at the top of the Offerwall. You can
set this as your app name or whatever you wish.
User-reward Unit in your App The virtual currency unit that you use inside the app.
User-reward Amount The conversion rate used to reward users points. You
will need to put in a value for the amount of virtual currency
to reward for every 100 points received from an offer.
e.g. if you input “20” here and your virtual currency unit is
“stars”, if you receive 100 points from the offer, the user
will receive 20 stars.
Reward Confirmation Metaps Server: Select this if you will receive Reward
Confirmation inside the app.
Notify Your Server: Select this is if Reward Confirmation
is to be sent to your server first.
Use SDK Decline fulfillment notification via the use of html tag
or SDK: Unused option.
Will notify Metaps of fulfillment via the use of html tag
or SDK: Please select this option.
Reward Confirmation URL If you have selected “Notify Your Server” in the Reward
Confirmation setting above, you will need to input your
server URL here to be notified of Reward Confirmation.
5. Displaying Offers with the SDK
Initialize the SDK Inside your Project
Note: This section on initialization is not required for developers using WebView, and
can skip to the next section on “Displaying Offers Using WebView"
Before using the SDK to display the Offerwall, you must first call the static method
startReward of the MetapsFactory class.
It is essential that the startReward method be called before any other processes are
executed. Therefore it is recommended that you call startReward in your Activity’s
onResume method.
About “Receiver”
The "Receiver" that appears in the sample code above assumes that you will implement the
net.Metaps.sdk.Receiver interface as "MyReceiver" (or whatever class name you want).
Inside "MyReceiver", you will need to create the Java code that processes the received
points from Metaps, and give virtual currency or unlock rewards for the end-user.14
For further details, look at the section, "Receiving Reward Confirmation" under the chapter
"Receiving Reward Confirmation in Your App".
About the parameters for the startReward method
Inside the SDK zip file you will find a JavaDoc under the “doc” folder. For more details on
the parameters, please refer to the JavaDoc.
About “end-user ID”
For the end-user ID, please pass a unique ASCII string, no longer than 32 characters in
length to identify each end-user in your application. If the notification for Reward
Confirmation is to be sent to your server, it is required to pass this end-user ID string at
some point. (i.e. at initialization or before displaying the Offerwall)
It is possible to perform initialization while passing an empty string or null value, but be sure
to pass a valid value by the time your display the Offerwall.
About “scenario”
The scenario string is an ASCII string with a maximum length of 64 characters. Depending
on your app’s calling activity, this can be used to branch out the process for receiving
Reward Confirmation.
It is possible to perform initialization while passing an empty string or null value at this point.
Displaying the Offerwall from a Native App
The ways in which to show Reward Ads are highly customizable, and can be tailored to
meet the needs of your app. Although the Offerwall can be called in different ways, the
sample code below illustrates one of the simpler ways this can be done.
Thứ Năm, 8 tháng 11, 2012
Working with Animations
In Chapter 3, you learned how to define animations as external resources. Now, you get the opportunity
to put them to use.
Android offers two kinds of animation:
➤ Frame-by-Frame Animations Traditional cell-based animations in which a different Draw-
able is displayed in each frame. Frame-by-frame animations are displayed within a View,
using its Canvas as a projection screen.
➤ Tweened Animations Tweened animations are applied to Views, letting you define a series
of changes in position, size, rotation, and opacity that animate the View contents.
Both animation types are restricted t o the original bounds of the View they’re
applied to. Rotations, translations, and sc aling transformations that extend beyond
the original boundaries of the View will result in the contents being clipped.
Introducing Tweened Animations
Tweened animations offer a simple way to provide depth, movement, or feedback to your users at a
minimal resource cost.
Using animations to apply a set of orientation, scale, position, and opacity changes is much less
resource-intensive than manually redrawing the Canvas to achieve similar effects, not to mention far
simpler to implement.
Tweened animations are commonly used to:
➤ Transition between Activities.
➤ Transition between layouts within an Activity.
➤ Transition between different content displayed within the same View.
➤ Provide user feedback such as:
➤ Indicating progress.
➤ ‘‘Shaking’’ an input box to indicate an incorrect or invalid data entry.
Creating Tweened Animations
Tweened animations are created using theAnimation class. The following list explains the animation
types available.
➤ AlphaAnimation Lets you animate a change in the View’s transparency (opacity or alpha
blending).
➤ RotateAnimation Lets you spin the selected View canvas in the XY plane.
➤ ScaleAnimation Allows you to zoom in to or out from the selected View.
➤ TranslateAnimation Lets you move the selected View around the screen (although it will
only be drawn within its original bounds).
Android offers the AnimationSetclass to group and configure animations to be run as a set. You can
define the start time and duration of each animation used within a set to control the timing and order
of the animation sequence.
It’s important to set the start offset and duration for each child animation, or they
will all start and complete at the same time.
Listings 15-12 and 15-13 demonstrate how to create the same animation sequence in code or as an
external resource.
LISTING 15-12: Creating a tweened animation in code
>>>>>>>>>>>>>>>>code
As you can see, it’s generally both easier and more intuitive to create your animation sequences using
an external animation resource.
Applying Tweened Animations
Animations can be applied to any View by calling its startAnimation method and passing in the Ani-
mation or Animation Set to apply.
Animation sequences will run once and then stop, unless you modify this behavior using the
setRepeatMode and setRepeatCount methods on the Animation or Animation Set. You can force an
animation to loop or repeat in reverse by setting the repeat mode of RESTARTor REVERSErespectively.
Setting the repeat count controls the number of times the animation will repeat.
Listing 15-14 shows an Animation that repeats indefinitely.
LISTING 15-14: Applying an Animation that loops continuously
myAnimation.setRepeatMode( Animation.RESTART);
myAnimation.setRepeatCount(Animation.INFINITE );
myView.startAnimation(myAnimation);
Using Animation Listeners
The AnimationListenerlets you create an event handler that’s fired when an animation begins or ends.
This lets you perform actions before or after an animation has completed, such as changing the View
contents or chaining multiple animations.
Call setAnimationListeneron an Animation object, and pass in a new implementation of
AnimationListener, overriding onAnimationEnd, onAnimationStart ,andonAnimationRepeat as
required.
Listing 15-15 shows the basic implementation of an Animation Listener.
>>>>>>>>>>>>>>>>>code
Animating Layouts and View Groups
A LayoutAnimationis used to animate View Groups, applying a single Animation (or Animation Set)
to each child View in a predetermined sequence.
Use aLayoutAnimationControllerto specify an Animation (or Animation Set) that’s applied to each
child View in a View Group. Each View it contains will have the same animation applied, but you can
use the Layout Animation Controller to specify the order and start time for each View.
Android includes twoLayoutAnimationControllerclasses.
➤ LayoutAnimationController Lets you select the start offset of each View (in milliseconds)
and the order (forward, reverse,andrandom ) to apply the animation to each child View.
➤ GridLayoutAnimationController Is a derived class that lets you assign the animation
sequence of the child Views using grid row and column references.
Creating Layout Animations
To create a new Layout Animation, start by defining the Animation to apply to each child View. Then
create a new LayoutAnimation, either in code or as an external animation resource, that references the
animation to apply and defines the order and timing in which to apply it.
Listing 15-16 show the definition of a simple Animation stored as popin.xml in the res/anim folder, and
a Layout Animation definition stored as popinlayout.xml.
The Layout Animation applies a simple ‘‘pop-in’’ animation randomly to each child View of any View
Group it’s assigned to.
>>>>>>>>>>>>>>>>>code
Using Layout Animations
Once you’ve defined a Layout Animation, you can apply it to a View Group either in code or in the
layout XML resource. In XML this is done using the android:layoutAnimation tag in the layout
definition:
android:layoutAnimation="@anim/popinlayout"
To set a Layout Animation in code, call setLayoutAnimation on the View Group, passing in a reference
to the LayoutAnimationobject you want to apply.
In each case, the Layout Animation will execute once, when the View Group is first laid out. You can
force it to execute again by calling scheduleLayoutAnimation on the ViewGroup object. The animation
will then be executed the next time the View Group is laid out.
Layout Animations also support Animation Listeners.
>>>>>>>>>>>>>>>code
Creating and Using Frame-by-Frame Animations
Frame-by-frame animations are akin to traditional cel-based cartoons in which an image is chosen for
each frame. Where tweened animations use the ta rget View to supply the content of the animation,
frame-by-frame animations let you specify a series of Drawable objects that are used as the background
to a View.
The AnimationDrawable class is used to create a new frame-by-frame animation presented as a
Drawable resource. You can define your Animation Drawa ble resource as an external resource in your
project’s res/drawable folder using XML.
Use the<animation-list>tagtogroupacollectionof <item>nodes, each of which uses a drawable
attribute to define an image to display, and a duration attribute to specify the time (in milliseconds) to
display it.
Listing 15-18 shows how to create a simple animation that displays a rocket taking off (rocket images
not included). The file is stored as res/drawable/animated_rocket.xml.
>>>>>>>>>>>>>Code
To display your animation, set it as the background to a View using the setBackgroundResource
method.
ImageView image = (ImageView)findViewById(R.id.my_animation_frame);
image.setBackgroundResource(R.drawable.animated_rocket);
Alternatively, use thesetBackgroundDrawable to use a Drawable instance instead of a resource refer-
ence. Run the animation calling its startmethod.
Thứ Tư, 7 tháng 11, 2012
14 Sensors
Sensors
WHAT’S IN THIS CHAPTER?
➤ Using the Sensor Manager
➤ The available sensor-types
➤ Monitoring sensors and interpreting sensor values
➤ Using the compass, accelerometer, and orientation sensors
➤ Remapping your orientation reference frame
➤ Controlling device vibration
Modern mobile phones are much more than simple communications devices with a connection
to the Internet. With microphones, cameras, accel erometers, compasses, temperature gauges,
and brightness detectors, smartphones have becomeextra-sensory devices, able to augment your
own perceptions.
Later chapters will explore use of the camera and microphone; in this chapter you will explore
the environmental sensors potentially available on Android devices.
Sensors that detect physical and environmental properties offer an exciting innovation for
enhancing the user experience of mobile applications. The incorporation of electronic com-
passes, gravity sensors, brightness gauges, and proximity sensors in modern devices provides an
array of new possibilities for interacting with devices, such as augmented reality and physical
movement-based input.
In this chapter you’ll be introduced to the sensors available in Android and how to use the Sen-
sor Manager to monitor them. You’ll take a closer look at the accelerometer and orientation
sensors and use them to determine changes in the device orientation and acceleration. This is
particularly useful for creating motion-based u ser interfaces, letting you add new dimensions to
your location-based applications.
You’ll also learn how to control device vibration to use force feedback in your applications.
USING SENSORS AND THE SENSOR MANAGER
The Sensor Manager is used to manage the sensor hardware available on Android devices. Use
getSystemService to return a reference to the Sensor Manager Service, as shown in the following
snippet:
String service_name = Context.SENSOR_SERVICE;
SensorManager sensorManager = (SensorManager)getSystemService(service_name);
Introducing Sensors
Like location-based Services, Android abstracts the sensor implementations of each device. The Sensor
class is used to describe the properties of each hardw are sensor, including its type, name, manufacturer,
and details on its accuracy and range.
The Sensor class includes a set of constants used to describe what type of hardware sensor is being
represented by a Sensor object. These constants take the form of Sensor.TYPE_<TYPE> . The follow-
ing section describes each supported sensor-type , after which you’ll learn how to find and use those
sensors.
Supported Android Sensors
The following is a list of the sensor-types currently available; note that the hardware on the host device
determines which of these sensors are actually available to your application.
➤ Sensor.TYPE_ACCELEROMETER A three-axis accelerometer sensor that returns the current
acceleration along three axes in m/s
2
. The accelerometer is explored in greater detail later
in this chapter.
➤ Sensor.TYPE_GYROSCOPE A gyroscopic sensor that returns the current device orientation on
three axes in degrees.
➤ Sensor.TYPE_LIGHT An ambient light sensor that returns a single value describing the ambi-
ent illumination in lux. A light sensor is commonly used to dynamically control the screen
brightness.
➤ Sensor.TYPE_MAGNETIC_FIELD A magnetic field sensor that finds the current magnetic field
in microteslas along three axes.
➤ Sensor.TYPE_ORIENTATION An orientation sensor that returns the device orientation on
three axes in degrees. The orientation sensor is explored in greater detail later in this chapter.
➤ Sensor.TYPE_PRESSURE A pressure sensor that returns a single value, the current pressure
exerted on the device in kilopascals.
➤ Sensor.TYPE_PROXIMITY A proximity sensor that indicates the distance between the device
and the target object in meters. How a target object is selected, and the distances supported,
will depend on the hardware implementation of the proximity detector. A typical use for the
proximity sensor is to detect when the device is being held up against the user’s ear and to
automatically adjust screen brightness or initiate a voice command.
Finding Sensors
An Android device can include multiple implementati ons of a particular sensor-type. To find the default
Sensor implementation for a particular type use the Sensor Manager’s getDefaultSensor method,
passing in the sensor-type required from the constants described in the previous section.
The following snippet returns the default gyroscope. If no default Sensor exists for the given type, the
method returns null.
Sensor defaultGyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
Alternatively, use getSensorList to return a list of all the available Sensors of a given type, as shown
in the following code, which returns all the available pressure sensor objects:
List<Sensor> pressureSensors = sensorManager.getSensorList(Sensor.TYPE_PRESSURE);
To find every Sensor available on the host platform use getSensorList , passing in Sensor.TYPE_ALL,
as shown here:
List<Sensor> allSensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
This technique lets you determine which Sensors, and sensor-types, are available on the host platform.
Using Sensors
Listing 14-1 shows the standard pattern for monitoring hardware sensor results. Later sections will
take a closer look at orientation and acceleration Sensor implementations in particular.
Implement a SensorEventListener.Usethe onSensorChangedmethod to monitor Sensor values and
onAccuracyChangedto react to changes in a Sensor’s accuracy.
LISTING 14-1:Sensor Event Listener skeleton code
final SensorEventListener mySensorEventListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent sensorEvent) {
// TODO Monitor Sensor changes.
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO React to a change in Sensor accuracy.
}
};
The SensorEvent parameter in theonSensorChangedmethod includes four properties used to describe
aSensorevent:
➤ sensor The Sensor object that triggered the event.
➤ accuracy The accuracy of the Sensor when the event occurred (low, medium, high, or unre-
liable, as described in the next list).
➤ values A float array that contains the new value(s) detected. The next section explains the
values returned for each sensor-type.
➤ timestamp The time (in nanoseconds) at which the Sensor event occurred.
You can monitor changes in the accuracy of a Sensor separately, using the onAccuracyChangedmethod.
In both handlers the accuracy value represents feedback from the monitored Sensor’s accuracy, using
one of the following constants:
➤ SensorManager.SENSOR_STATUS_ACCURACY_LOW Indicates that the Sensor is reporting with
low accuracy and needs to be calibrated
➤ SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM Indicates that the Sensor data is of aver-
age accuracy, and that calibration might improve the readings
➤ SensorManager.SENSOR_STATUS_ACCURACY_HIGH Indicates that the Sensor is reporting with
the highest possible accuracy
➤ SensorManager.SENSOR_STATUS_UNRELIABLE Indicates that the Sensor data is unreliable,
meaning that either calibration is required or readings are not currently possible
To receive Sensor events, register your Sensor Event Listener with the Sensor Manager. Specify the
Sensor object to observe, and the rate at which you want to receive updates. The following example
registers a Sensor Event Listener for the defau lt proximity Sensor at the normal update rate:
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
sensorManager.registerListener(mySensorEventListener,
sensor,
SensorManager.SENSOR_DELAY_NORMAL);
The Sensor Manager includes the following constants (shown in descending order of responsiveness) to
let you select a suitable update rate:
➤ SensorManager.SENSOR_DELAY_FASTEST Specifies the fastest possible Sensor update rate
➤ SensorManager.SENSOR_DELAY_GAME Selects an update rate suitable for use in controlling
games
➤ SensorManager.SENSOR_DELAY_NORMAL Specifies the default update rate
➤ SensorManager.SENSOR_DELAY_UI Specifies a rate suitable for updating UI features
The rate you select is not binding; the Sensor Manager may return results faster or slower than you
specify, though it will tend to be faster. To minimize the associated resource cost of using the Sensor in
your application you should try to select the slowest suitable rate.
It’s also important to unregister your Sensor Event L isteners when your application no longer needs to
receive updates:
sensorManager.unregisterListener(mySensorEventListener);
It’s good practice to register and unregister your Sensor Event Listener in the onResume and onPause
methods of your Activities to ensure they’re being used only when the Activity is active.
INTERPRETING SENSOR VALUES
The length and composition of the values returned in the onSensorChangedevent vary depending on
the Sensor being monitored.
The details are summarized in Table 14-1. Further details on the use of the accelerometer, orientation,
and magnetic field Sensors can be found in the following sections.
The Android documentation describes thevalues returned by each sensor-type with
some additional commentary at http://developer.android.com/reference/
android/hardware/Sensor.html
TABLE 14-1: Sensor Return Values
SENSOR-TYPE VALUE COUNT VALUE COMPOSITION COMMENTARY
TYPE_ACCELEROMETER 3 value[0] : Lateral
value[1] : Longitudinal
value[2] : Vertical
Acceleration along three
axes in m/s
2
. The Sensor
Manager includes a set of
gravity constants of the form
SensorManager.GRAVITY_*
TYPE_GYROSCOPE 3 value[0] : Azimuth
value[1] : Pitch
value[2] : Roll
Device orientation in degrees
along three axes.
TYPE_ LIGHT 1 value[0] : Illumination Measured in lux. The Sensor
Manager includes a set of con-
stants representing different
standard illuminations of the
form SensorManager.LIGHT_*
TYPE_MAGNETIC_FIELD 3 value[0] : Lateral
value[1] : Longitudinal
value[2] : Vertical
Ambient magnetic field mea-
sured in microteslas (μT).
TYPE_ORIENTATION 3 value[0] : Azimuth
value[1] : Roll
value[2] : Pitch
Device orientation in degrees
along three axes.
TYPE_PRESSURE 1 value[0] : Pressure Measured in kilopascals (KP).
TYPE_PROXIMITY 1 value[0] : Distance Measured in meters.
TYPE_TEMPERATURE 1 value[0] : Temperature Measured in degrees Celsius.
Đăng ký:
Bài đăng (Atom)